LeetCode: String to Integer (atoi) 解题报告
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.
spoilers alert... click to show requirements for atoi.
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.
Solution
1. 用String s = str.trim();裁掉前面后面的空格。
2. 记录起始的+,-符号。
3. 用Long来转数字,这样的话就算越界也没什么了。
1 public class Solution { 2 public int atoi(String str) { 3 if (str == null) { 4 return 0; 5 } 6 7 // remove the spaces in the begining and the end. 8 String s = str.trim(); 9 10 boolean minus = false; 11 long num = 0; 12 for (int i = 0; i < s.length(); i++) { 13 /* 14 takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. 15 */ 16 if (i == 0 && s.charAt(i) == '+') { 17 continue; 18 } else if (i == 0 && s.charAt(i) == '-'){ 19 // get the 20 minus = true; 21 continue; 22 } 23 24 int c = s.charAt(i) - '0'; 25 if (c > 9 || c < 0) { 26 // invalid character. 27 break; 28 } 29 30 num = num * 10 + c; 31 } 32 33 // If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is // returned. 34 if (minus) { 35 num = -num; 36 num = Math.max(num, Integer.MIN_VALUE); 37 } else { 38 num = Math.min(num, Integer.MAX_VALUE); 39 } 40 41 return (int)num; 42 } 43 }
2015.1.3 redo,
leetcode升级了test case,加入了更大的比long还大的数。这样我们必须在循环里就判断是不是越界就退出了,否则long也会爆掉的。
1 public class Solution { 2 public int atoi(String str) { 3 long ret = 0; 4 5 // ___+1234__ 6 // Delete the leading and tailing spaces. 7 String sNew = str.trim(); 8 9 if (sNew.length() == 0) { 10 return 0; 11 } 12 13 boolean positive = true; 14 for (int i = 0; i < sNew.length(); i++) { 15 char c = sNew.charAt(i); 16 if (i == 0 && c == '+') { 17 continue; 18 } else if (i == 0 && c == '-') { 19 positive = false; 20 continue; 21 } 22 23 if (!(c <= '9' && c >= '0')) { 24 break; 25 } 26 27 int dig = positive ? c - '0': '0' - c; 28 29 ret = ret * 10 + dig; 30 31 // bug 2: should consider the out of range. 32 if (ret > Integer.MAX_VALUE) { 33 return Integer.MAX_VALUE; 34 } else if (ret < Integer.MIN_VALUE) { 35 return Integer.MIN_VALUE; 36 } 37 } 38 39 return (int)ret; 40 } 41 }
GitHub
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/Atoi.java
posted on 2014-10-21 11:32 Yu's Garden 阅读(720) 评论(0) 编辑 收藏 举报