leetcode -- 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.
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.
本题需要注意如下几点:
- 开头有任意多个空格,需要trim
- 数字开头可能有符号,+,—
- 输入可能不是一个整数,如12dds返回12;abc返回0
- 输入整数可能会超过整数表示范围,如超过则返回 INT_MAX (2147483647) or INT_MIN (-2147483648)
1 public class Solution { 2 public int atoi(String str) { 3 // Start typing your Java solution below 4 // DO NOT write main() function 5 str = str.trim(); 6 String[] integers = str.split(" "); 7 if(integers[0].equals("")) 8 return 0; 9 String s1 = integers[0]; 10 boolean positiveFlag = true; 11 if(s1.charAt(0) == '+'){ 12 s1 = s1.substring(1); 13 } else if(s1.charAt(0) == '-'){ 14 positiveFlag = false; 15 s1 = s1.substring(1); 16 } 17 long result = 0; 18 for(int i = 0; i < s1.length(); i++){ 19 if(s1.charAt(i) >= 48 && s1.charAt(i) <= 57) 20 result = result * 10 + (s1.charAt(i) - 48); 21 else 22 break; 23 } 24 25 if(!positiveFlag) 26 result = result * -1; 27 28 if(result > 2147483647) 29 return 2147483647; 30 31 if(result < -2147483648) 32 return -2147483648; 33 34 return (int)result; 35 45 } 46 }
refactor code:
1 public int atoi(String str) { 2 // Note: The Solution object is instantiated only once and is reused by each test case. 3 if(str == null || str.length() == 0){ 4 return 0; 5 } 6 7 str = str.trim(); 8 boolean positiveFlag = true; 9 if(str.charAt(0) == '-'){ 10 positiveFlag = false; 11 str = str.substring(1); 12 } else if(str.charAt(0) == '+'){ 13 positiveFlag = true; 14 str = str.substring(1); 15 } 16 17 long result = 0; 18 for(int i = 0; i < str.length(); i++){ 19 char c = str.charAt(i); 20 if(c >= '0' && c <= '9'){ 21 result = 10 * result + (c - '0'); 22 } else { 23 break; 24 } 25 } 26 if(!positiveFlag){ 27 result = -1 * result; 28 } 29 30 if(result < Integer.MIN_VALUE){ 31 return Integer.MIN_VALUE; 32 } 33 if(result > Integer.MAX_VALUE){ 34 return Integer.MAX_VALUE; 35 } 36 return (int)result; 37 }