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.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package leetcode; | |
/** | |
* Solution: careful! + corner case | |
* 1. first digit -- positive or negative. | |
* 2. Integer Boundary. | |
* | |
* 这类题目还有Reverse Integer,都是考察corner case,当然也要把符号和非number digit 考虑进去,两个边界的处理一定要熟. | |
* @author jeffwan | |
* @date Apr 14, 2014 | |
*/ | |
public class Atoi { | |
public static int atoi(String str) { | |
if (str == null) { | |
return 0; | |
} | |
str = str.trim(); | |
if (str.length() == 0) { | |
return 0; | |
} | |
int i = 0; | |
boolean positive = true; | |
if (str.charAt(0) == '-' || str.charAt(0) == '+') { | |
if (str.charAt(0) == '-') { | |
positive = false; | |
} | |
i = 1; | |
} | |
long sum = 0; | |
for (; i < str.length(); i++) { | |
int temp = str.charAt(i) - '0'; | |
if (temp < 0 || temp > 9) { | |
break; | |
} | |
sum = sum * 10 + temp; | |
if (sum > Integer.MAX_VALUE) { | |
break; | |
} | |
} | |
// Handle < -2147483647 case | |
if (sum > Integer.MAX_VALUE && !positive) { | |
return Integer.MIN_VALUE; | |
} | |
// Handle > 2147483647 case | |
if (sum >= Integer.MAX_VALUE && positive) { | |
System.out.println(sum); | |
return Integer.MAX_VALUE; | |
} | |
if (!positive) { | |
sum = -sum; | |
} | |
return (int) sum; | |
} | |
// solution2 : use Integet.MAX_VALUE / 10 as condition. | |
public int atoi(String str) { | |
if (str == null || str.length() == 0) { | |
return 0; | |
} | |
str = str.trim(); | |
int result = 0; | |
int i = 0; | |
boolean positive = true; | |
if (str.charAt(0) == '-' || str.charAt(0) == '+') { | |
if (str.charAt(0) == '-') { | |
positive = false; | |
} | |
i = 1; | |
} | |
for (; i < str.length(); i++) { | |
if (!Character.isDigit(str.charAt(i))) { | |
break; | |
} | |
int digit = Character.getNumericValue(str.charAt(i)); | |
if (positive && result > Integer.MAX_VALUE / 10) { | |
return Integer.MAX_VALUE; | |
} | |
if (positive && result == Integer.MAX_VALUE / 10 && digit >= 8) { | |
return Integer.MAX_VALUE; | |
} | |
if (!positive && result > Integer.MAX_VALUE / 10) { | |
return Integer.MIN_VALUE; | |
} | |
if (!positive && result == Integer.MAX_VALUE / 10 && digit >= 9) { | |
return Integer.MIN_VALUE; | |
} | |
result = result * 10; | |
result += digit; | |
} | |
return positive ? result : -result; | |
} | |
} |
No comments:
Post a Comment