8. String to Integer (atoi)

Implement atoi which converts a string to an integer.

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned.

Note:

  • Only the space character ' ' is considered as whitespace character.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.

Example 1:

Input: "42"
Output: 42

Example 2:

Input: "   -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
             Then take as many numerical digits as possible, which gets 42.

Example 3:

Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.

Example 4:

Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical 
             digit or a +/- sign. Therefore no valid conversion could be performed.

Example 5:

Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
             Thefore INT_MIN (−231) is returned.

不知道有多少人看见题目和我一个想法的,内容太多不想看,还全是英语。


没关系不看就不看吧,我们看能看懂的地方,或者有兴趣的同学可以自行百度。

例子1 输入字符串"42"输出数字 42

例子2 输入字符串"     -42" 输出数字 -42

例子3 输入字符串"4193 with words" 输出数字 4193

例子4 输入字符串"words and 987" 输出数字0

例子5 输入字符串"-91283472332" 输出数字-2147483648

大概看明白了吗?这道题在LeetCode中属于中等难度的题目,但是其实难度还是挺简单的,莫非难度都是针对咋们中国人?和考英语一样,连题目都看不懂,那这题目是难是简单对于我们来说有什么区别呢?

简单说下题目要求,给你一个字符串,找到第一个非空字符,如果这个字符是除了数字和"+","-"号之外的字符,那么返回0,否则尽可能多的与后面连续数字组合起来,这个数字作为整数值。然后从例子5中看出,如果这个数字大小超出整数范围,则根据符号返回最大整数或者最小整数。

fun myAtoi(str: String): Int {
    var i = 0
    while (i < str.length && str[i] == ' '){
        i++
    }
    if (str.length <= i) return 0
    if (str[i] != '+' && str[i] != '-' && str[i] !in '0'..'9') return 0

    //确定符号
    val minus = str[i] == '-'
    //确定数字起始位置
    var startIndex = if (str[i] == '+' || str[i] == '-'){
        i + 1
    }else{
        i
    }

    var result = 0
    while (startIndex < str.length && str[startIndex] in '0'..'9'){
        val num = str[startIndex] - '0'
        //判断是否超出范围
        if (result > (Int.MAX_VALUE - num) / 10)
            //这里记录一个坑,注意判断条件不要写成 result * 10 > Int.MAX_VALUE - num
            //因为如果 result * 10 超出Int的范围时计算就不在正确了
            return if (minus) Int.MIN_VALUE else Int.MAX_VALUE

        result = result * 10 + num

        startIndex++
    }
    return if (minus) result * -1 else result
}

没什么太复杂的东西,主要就是拆解字符,然后多加判断就好了

点击查看更多解法


猜你喜欢

转载自blog.csdn.net/z89135898/article/details/80268056