题目
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.因为数字前面的空格是允许的,所以上来可以先将空格跳过,让 i 指向第一个不是空格的字符。
而且要考虑全都是空格的情况。如果全是空格的这种情况,i 在最后还会在加一次,即 i 的值等于len(charArray.length), 此时已经越界了。所以在这里又判断了一下。
if(i == len) return 0;
2.如果发现了符号,再将遇到的+、-号记录下来,这里是用negative变量记录,如果遇到了 - ,直接让negative==-1,最后的时候直接返回result*negative就行了。
注意:这里是用的if...else if...
即两个条件里面只能选一个(只选最前面的一个符号就好了)。
比如"+,-100"(不加,号就是这样±…),但这不是有效的数字。之前只是用两个if
判断,判断+后正好i++,接着判断-,最后会返回个-100,但正确的是应该返回0;
3.接下来往后遍历,只要不是数字就直接跳出循环。
举几个例子可能清楚些:" kij-100" , " -iii123" , " 123kkk"," 123k3kk" 反正数字前面不能有字母,数字又得是连着的,所以只要不是数字跳出来就行。
之前是在循环外面再判断是不是溢出了,然后再return Integer.MAX_VALUE
或是 Integer.MIN_VALUE
但比如:"-91283472332" result的值加到912834723的时候再往下加一位,会变成另一个数,出现精度的问题。所以可以在循环里面判断
if(result > Integer.M AX_VALUE/10 || (result == Integer.MAX_VALUE/10 && Integer.MAX_VALUE % 10 < (charArray[i] - '0')) )
如果result 没 乘10 之前就 > Integer.MAX_VALUE/10,那乘个10不比它更大。
又或者
result的值刚好等于 Integer.MAX_VALUE/10 ,那比较个位就行了。
把符号去掉后Integer.MIN_VALUE == Integer.MAX_VALUE +1,
-2147483648
2147483647
之前想着:这个判断时候越界是根据 Int 的最大值,但去掉符号后最大值和最小值不一样啊,就是如果 一个数 > Integer.MAX_VALUE 但 < Integer.MIN_VALUE, 就是相对与最大值越界了,但相对最小值没越界。。。大哥,这是int啊,MAX和MIN只相差1,就算是相对MIN没越界,个位也只能是个8了,这个时候返回的不还是Integer.MIN_VALUE
java代码
class Solution {
public int myAtoi(String str) {
char[] charArray = str.toCharArray();
int len = charArray.length;
int result = 0;
int negative = 1;
int i = 0;
while(i < len && charArray[i] == ' ') {
i++;
}
if(i == len)
return 0;
if(charArray[i] == '+' && i != len - 1)
i++;
else if(charArray[i] == '-' && i != len - 1) {
negative = -1;
i++;
}
for(;i < len; i++){
if(charArray[i] < '0' || charArray[i] > '9')
break;
if(result > Integer.MAX_VALUE/10 || (result == Integer.MAX_VALUE/10 && Integer.MAX_VALUE % 10 < (charArray[i] - '0')) )
return negative == -1 ? Integer.MIN_VALUE : Integer.MAX_VALUE;
result = result * 10 + (charArray[i] - '0');
}
return result*negative;
}
}
第一次写,感觉思路的杂在一起,麻麻咧咧的,一点都不圆润,通透。。。。