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.
Input: | "-2147483647" |
Output: | -2147483648 |
Expected: | -2147483647 |
Input: | "-+1" |
Output: | -2147483648 |
Expected: | 0 |
Input: | "-1" |
Output: | -2147483648 |
Expected: | -1 |
Runtime Error Message: | Line 38: java.lang.NumberFormatException: For input string: "09223372036854775809" |
Last executed input: | "9223372036854775809" |
Runtime Error Message: | Line 23: java.lang.NumberFormatException: For input string: "011919730356" |
Last executed input: | " -11919730356x" |
Runtime Error Message: | Line 44: java.lang.NumberFormatException: For input string: "" |
Last executed input: | "" |
Input: | " - 321" |
Output: | -321 |
Expected: | 0 |
题目不难,将字符串转换为数字,但是需要考虑到多种情况,用血的教训告诉我有多种边界条件没考虑。(适合测试人员面试)
package leetcode;
public class String2IntegerAtoi {
public static int myAtoi(String str) {
char[] chars = str.toCharArray();
String ans = "";
boolean judge = true; //判断返回对是正数还说负数
int countSign = 0; //判断正负号出现次数,如果大于1,则输出0;
int countSpace = 0; //判断空格间断出现次数,如果大于1,则输出前面对数;
for (int i = 0; i<chars.length; i++){
if (chars[i] == ' ' && countSpace == 0){
continue;
}
if (chars[i] == '+' && countSign == 0){
judge = true;
countSpace ++;
countSign ++;
continue;
}
if (chars[i] == '-' && countSign == 0){
//System.out.println(chars[i] + " " + countSign);
judge = false;
countSpace ++;
countSign ++;
continue;
}
if (chars[i]< '0' || chars[i] > '9'){
//System.out.println(chars[i] + " "+ countSign);
if (judge){
if (ans.length() > 10) return Integer.MAX_VALUE;
if (ans.length() == 0) return 0;
if (Long.parseLong(ans) >= Integer.MAX_VALUE)
return Integer.MAX_VALUE;
return Integer.parseInt(ans);
}
else {
if (ans.length() > 10) return -Integer.MIN_VALUE;
if (ans.length() == 0) return 0;
if (Long.parseLong("-"+ans) <= Integer.MIN_VALUE)
return -Integer.MIN_VALUE;
return -Integer.parseInt(ans);
}
}
if (chars[i] >= '0' && chars[i] <= '9' ){
countSpace ++;
ans = ans + chars[i];
}
}
//System.out.println(ans);
if (judge){
if (ans.length() > 10) return Integer.MAX_VALUE;
if (ans.length() == 0) return 0;
if (Long.parseLong(ans) >= Integer.MAX_VALUE)
return Integer.MAX_VALUE;
return Integer.parseInt(ans);
}
else {
if (ans.length() > 10) return -Integer.MIN_VALUE;
if (ans.length() == 0) return 0;
if (Long.parseLong("-"+ans) <= Integer.MIN_VALUE)
return -Integer.MIN_VALUE;
return -Integer.parseInt(ans);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(myAtoi(" - 321"));
//System.out.println(Integer.MIN_VALUE);
}
}