|
| 1 | +# [String to Integer (atoi)][title] |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +Implement `atoi` to convert a string to an integer. |
| 6 | + |
| 7 | +**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. |
| 8 | + |
| 9 | +**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. |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +**Requirements for atoi:** |
| 14 | + |
| 15 | +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. |
| 16 | + |
| 17 | +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. |
| 18 | + |
| 19 | +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. |
| 20 | + |
| 21 | +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. |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +## 思路 |
| 26 | + |
| 27 | +1. 忽略前面的连续空格 |
| 28 | +2. 处理第一个非空字符,如果是 + 或者 -,记录一个标志位 |
| 29 | +3. 然后依次读取字符,如果是非数字字符或者数值超过 INT 最大值退出 |
| 30 | +4. 结果乘以标志位,和 INT 最大值、INT 最小值进行比较 |
| 31 | + |
| 32 | +## [完整代码][src] |
| 33 | + |
| 34 | +```java |
| 35 | +class Solution { |
| 36 | + public int myAtoi(String str) { |
| 37 | + if (str == null || str.length() == 0) { |
| 38 | + return 0; |
| 39 | + } |
| 40 | + |
| 41 | + int len = str.length(); |
| 42 | + int i = 0; |
| 43 | + |
| 44 | + while (i < len && str.charAt(i) == ' ') { |
| 45 | + i++; |
| 46 | + } |
| 47 | + |
| 48 | + if (i >= len) { |
| 49 | + return 0; |
| 50 | + } |
| 51 | + |
| 52 | + char sign = str.charAt(i); |
| 53 | + int flag = 1; |
| 54 | + if (sign == '-' || sign == '+') { |
| 55 | + flag = (sign == '-') ? -1 : 1; |
| 56 | + i++; |
| 57 | + } |
| 58 | + |
| 59 | + long result = 0; |
| 60 | + while (i < len) { |
| 61 | + char c = str.charAt(i++); |
| 62 | + if (c < '0' || c > '9' || result > Integer.MAX_VALUE) { |
| 63 | + break; |
| 64 | + } |
| 65 | + result = result * 10 + c - '0';s |
| 66 | + } |
| 67 | + |
| 68 | + result *= flag; |
| 69 | + if (result > Integer.MAX_VALUE) { |
| 70 | + return Integer.MAX_VALUE; |
| 71 | + } else if (result < Integer.MIN_VALUE) { |
| 72 | + return Integer.MIN_VALUE; |
| 73 | + } |
| 74 | + |
| 75 | + return (int)result; |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +[title]: https://leetcode.com/problems/string-to-integer-atoi |
| 81 | +[src]: https://github.com/andavid/leetcode-java/blob/master/src/com/andavid/leetcode/_008/Solution.java |
0 commit comments