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.
一个atoi,,要求写了长篇大论,但是下面都是要求,比如越界如何处理,如果有空格,如果有字母等等
一个一个看太麻烦了,先实现一个大概了,看具体Test Case哪些没满足要求
package com.maoxiaomeng;
/**
* @author lihui
* @date 2018/3/26 12:31
*/
public class Solution {
public int myAtoi(String str) {
if (str == null || str.length() < 1 || str.equals("+") || str.equals("-")) {
return 0;
}
str = str.trim();
long temp = 0;
int pos = 0;
int flag = 0;
if (str.charAt(0) == '-') {
flag = -1;
pos ++;
} else if(str.charAt(0) == '+') {
flag = 1;
pos ++;
}
for (; pos < str.length(); pos++) {
if (Character.isDigit(str.charAt(pos))) {
temp = temp * 10 + (str.charAt(pos) - '0');
}
}
if (flag == -1) {
temp = -temp;
}
if (temp > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
} else if (temp < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
return (int)temp;
}
}
但奇葩的是,+-2输出的结果应该是0
Submission Result: Wrong Answer More Details
这么看来意思应该是,字符串除了首末的空格以及首位的符号位之外,其它任何位置上如果出现了非数字,就直接break,截断,只统计前面的数字;如此一来,for循环里加一个else,不满足就break跳出
package com.maoxiaomeng;
/**
* @author lihui
* @date 2018/3/26 12:31
*/
public class Solution {
public int myAtoi(String str) {
if (str == null || str.length() < 1 || str.equals("+") || str.equals("-")) {
return 0;
}
str = str.trim();
long temp = 0;
int pos = 0;
int flag = 0;
if (str.charAt(0) == '-') {
flag = -1;
pos ++;
} else if(str.charAt(0) == '+') {
flag = 1;
pos ++;
}
for (; pos < str.length(); pos++) {
if (Character.isDigit(str.charAt(pos))) {
temp = temp * 10 + (str.charAt(pos) - '0');
} else {
break;
}
}
if (flag == -1) {
temp = -temp;
}
if (temp > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
} else if (temp < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
return (int)temp;
}
}
越界的错误,输入的字符串对应数字连Long都超过了
Submission Result: Wrong Answer More Details
将temp的类型由long改成double即可
package com.maoxiaomeng;
/**
* @author lihui
* @date 2018/3/26 12:31
*/
public class Solution {
public int myAtoi(String str) {
if (str == null || str.length() < 1 || str.equals("+") || str.equals("-")) {
return 0;
}
str = str.trim();
double temp = 0;
int pos = 0;
int flag = 0;
if (str.charAt(0) == '-') {
flag = -1;
pos ++;
} else if(str.charAt(0) == '+') {
flag = 1;
pos ++;
}
for (; pos < str.length(); pos++) {
if (Character.isDigit(str.charAt(pos))) {
temp = temp * 10 + (str.charAt(pos) - '0');
} else {
break;
}
}
if (flag == -1) {
temp = -temp;
}
if (temp > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
} else if (temp < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
return (int)temp;
}
}
顺便可以看看他们的取值范围
System.out.println("Long_MAX: " + Long.MAX_VALUE);
System.out.println("Long_MIN: " + Long.MIN_VALUE);
System.out.println("Double_MAX: " + Double.MAX_VALUE);
System.out.println("Double_MIN: " + Double.MIN_VALUE);
Long_MAX: 9223372036854775807
Long_MIN: -9223372036854775808
Double_MAX: 1.7976931348623157E308
Double_MIN: 4.9E-324
OVER