题目描述:
Implement atoi toconvert 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 giveninput specs). You are responsible to gather all the input requirements upfront.
*******************************************************************************
Requirements for atoi:
The function first discards as manywhitespace characters as necessary until the first non-whitespace character isfound. Then, starting from this character, takes an optional initial plus orminus sign followed by as many numerical digits as possible, and interpretsthem as a numerical value.
The string can contain additional charactersafter those that form the integral number, which are ignored and have no effecton the behavior of this function.
If the first sequence of non-whitespacecharacters in str is not a valid integral number, or if no such sequence existsbecause either str is empty or it contains only whitespace characters, noconversion is performed.
If no valid conversion could be performed, azero value is returned. If the correct value is out of the range ofrepresentable values, INT_MAX (2147483647) or INT_MIN (-2147483648) isreturned.
题目理解:
将字符串转换成整型数字
题目分析:
考虑以下情况:
1. 非数字字符在数字前,忽略非数字字符
2. 非数字字符在数字后,只考虑之前的数字
3. 考虑带正负号的字符串
4. 考虑以“0”为首的数字字符串
5. 考虑空白字符
6. 考虑空字符串
7. 考虑超出Integer范围的数字字符串
我的解答:
static public Integer atoi(String str) {
char[] tmp = str.toCharArray();
int i = 0;
int first = 0;
int end = str.length();
if (str == null) {
return 0;
}
//move the sapce
while (i < end && tmp[i] == ' ') {
i++;
}
first = i;
if (first == end) {
return 0;
}
if ((tmp[first] <= '9' && tmp[first] >= '0') || (tmp[first] == '+') || (tmp[first] == '-')) {
//the first is number or '+' or '-', continue
i++;
while (i < end) {
if (tmp[i] <= '9' && tmp[i] >= '0') {
//the tmp[i] is number, continue
i++;
continue;
} else {
end = i;
break;
}
}
} else {
return 0;
}
if (tmp[first] == '+') {
if (end - first == 1)
return 0;
if (end - first > 11)
return Integer.MAX_VALUE;
if (end - first == 11 && (str.substring(first, end).compareTo("+2147483647")) > 0)
return Integer.MAX_VALUE;
return Integer.parseInt(str.substring(first, end));
}
else if (tmp[first] == '-') {
if (end - first == 1)
return 0;
if (end - first > 11)
return Integer.MIN_VALUE;
if (end - first == 11 && str.substring(first, end).compareTo("-2147483648") > 0)
return Integer.MIN_VALUE;
return Integer.parseInt(str.substring(first, end));
}
else{
if (end - first > 10)
return Integer.MAX_VALUE;
if (end - first == 10 && (str.substring(first, end).compareTo("2147483647")) > 0)
return Integer.MAX_VALUE;
return Integer.parseInt(str.substring(first, end));
}
}
更好的解答1:
public static int myAtoi(String str) {
if (str == null || str.length() == 0)
return 0;//
str = str.trim();
char firstChar = str.charAt(0);
int sign = 1, start = 0, len = str.length();
long sum = 0;
if (firstChar == '+') {
sign = 1;
start++;
} else if (firstChar == '-') {
sign = -1;
start++;
}
for (int i = start; i < len; i++) {
if (!Character.isDigit(str.charAt(i)))
return (int) sum * sign;
sum = sum * 10 + str.charAt(i) - '0';
if (sign == 1 && sum > Integer.MAX_VALUE)
return Integer.MAX_VALUE;
if (sign == -1 && (-1) * sum < Integer.MIN_VALUE)
return Integer.MIN_VALUE;
}
return (int) sum * sign;
}
更好的解答2:
I think we only need to handle four cases:
1. discardsall leading whitespaces
2. sign ofthe number
3. overflow
4. invalidinput
static public int atoi(const char *str) {
int sign = 1, base = 0, i = 0;
while (str[i] == ' ') { i++; }
if (str[i] == '-' || str[i] == '+') {
sign = 1 - 2 * (str[i++] == '-');
}
while (str[i] >= '0' && str[i] <= '9') {
if (base > INT_MAX / 10 || (base == INT_MAX / 10 && str[i] - '0' > 7)) {
if (sign == 1) return INT_MAX;
else return INT_MIN;
}
base = 10 * base + (str[i++] - '0');
}
return base * sign;
}