8. String to Integer (atoi)
题目:
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
Update (2015-02-10):
The signature of the C++
function had been updated. If you still see your function signature accepts a const char *
argument, please click the reload button to reset your code definition.
spoilers alert... click to show requirements for atoi.
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. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
链接:https://leetcode.com/problems/string-to-integer-atoi/#/description
4/9/2017
不想自己做这道题了,是在凑答案的感觉。看别人答案之后做的。
第15行,注意当接近最大/最小值的时候,注意比较最末尾是否是8或9: 因为最大值是2,147,483,647
1 public class Solution { 2 public int myAtoi(String str) { 3 int i = 0; 4 int value = 0; 5 int sign = 1; 6 int prev_max = Integer.MAX_VALUE / 10; 7 while (i < str.length() && str.charAt(i) == ' ') i++; 8 if (i < str.length() && (str.charAt(i) == '+' || str.charAt(i) == '-')) { 9 if (str.charAt(i) == '-') sign = -1; 10 i++; 11 } 12 while (i < str.length()) { 13 int c = str.charAt(i) - '0'; 14 if (!Character.isDigit(str.charAt(i))) break; 15 if (value > prev_max || value == prev_max && c > 7) return sign == 1? Integer.MAX_VALUE: Integer.MIN_VALUE; 16 value = value * 10 + c; 17 i++; 18 } 19 return sign * value; 20 } 21 }
别人的做法:
链接:https://discuss.leetcode.com/topic/12473/java-solution-with-4-steps-explanations/6
1 public int myAtoi(String str) { 2 int i = 0; 3 str = str.trim(); 4 char[] c = str.toCharArray(); 5 6 int sign = 1; 7 if (i < c.length && (c[i] == '-' || c[i] == '+')) { 8 if (c[i] == '-') { 9 sign = -1; 10 } 11 i++; 12 } 13 14 int num = 0; 15 int bound = Integer.MAX_VALUE / 10; 16 while (i < c.length && c[i] >= '0' && c[i] <= '9') { 17 int digit = c[i] - '0'; 18 if (num > bound || (num == bound && digit > 7)) { 19 return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE; 20 } 21 num = num * 10 + digit; 22 i++; 23 } 24 return sign * num; 25 }
更多讨论:
https://discuss.leetcode.com/category/16/string-to-integer-atoi