一 题目
Implement atoi
which converts a string to an integer.
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.
Note:
- Only the space character
' '
is considered as whitespace character. - Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.
Example 1:
Input: "42" Output: 42
Example 2:
Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42.
Example 3:
Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
Example 4:
Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed.
Example 5:
Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned.
二 分析
题目大意就是把字符串转换为integer,允许的是空格、+、-以及数字。后面的例子给出了各种case。
各种变态的case,如:" +0 123" 要求输出0 而不是123。
1. 若字符串开头是空格,则跳过所有空格,到第一个非空格字符,如果没有,则返回0.
2. 若第一个非空格字符是符号 +/-,则标记 sign 的真假。
3. 若下一个字符不是数字,则返回0.。
4. 如果下一个字符是数字,则转为整形存下来,若接下来再有非数字出现,则返回目前的结果。
5. 还需要考虑边界问题,如果超过了整型数的范围,则用边界值替代当前值。我是用long的来判断的。
我在调试的时候各种踩坑,一度想要放弃,明明不难的一个题咋咋就这么多边界过不了,程序里面的写的比较啰嗦。
我也是需要在看看大神写的如何精炼的判断。
public static void main(String[] args) {
int num7 = myAtoi("-5-");
System.out.println(num7);
int num1 = myAtoi("0-1");
System.out.println(num1);
int num6 = myAtoi("1a");
System.out.println(num6);
int num5 = myAtoi(" 0000000000000 ");
System.out.println(num5);
int num0 = myAtoi("-000000000000001");
System.out.println(num0);
int num = myAtoi("-abc");
System.out.println(num);
int num2 = myAtoi("4193 with words");
System.out.println(num2);
int num3 = myAtoi("words and 987");
System.out.println(num3);
int num4 = myAtoi("-88827 5655 U");
System.out.println(num4);
}
public static int myAtoi(String str) {
while(str.startsWith(" ") ){
str = str.substring(1);
}
if(str.equals("")||str.equals("+")||str.equals("-")
||str.startsWith("+-")||str.startsWith("-+")){
return 0;
}
int res =0;
String sign="";
//数字开头
if(str.startsWith("+")|| str.startsWith("-")|| (str.charAt(0)>='0'&&str.charAt(0)<='9')){
if(str.startsWith("+")|| str.startsWith("-")){
sign = str.substring(0,1);
}
for(int i=1;i< str.length(); i++){
if(!Character.isDigit(str.charAt(i))){
str = str.substring(0, i);
}
}
String tmp = str.substring(1);
if(sign.equals("+")||sign.equals("-")){
while(tmp.startsWith("0") ){
tmp = tmp.substring(1);
}
str = sign+tmp;
}else{
while(str.startsWith("0") ){
str = str.substring(1);
}
}
if(str.equals("")|| str.equals(" ")||str.equals("+")||str.equals("-")){
return 0;
}
//防止越界
if(str.length()>11){
return str.startsWith("-")? Integer.MIN_VALUE:Integer.MAX_VALUE;
}
Long l = Long.parseLong(str);
if(l> Integer.MAX_VALUE){
res = Integer.MAX_VALUE;
}else if(l< Integer.MIN_VALUE ){
res = Integer.MIN_VALUE;
}else{
res = l.intValue();
}
return res;
}else{
return 0;
}
}
Runtime: 3 ms, faster than 25.52% of Java online submissions for String to Integer (atoi).
Memory Usage: 36.3 MB, less than 100.00% of Java online submissions forString to Integer (atoi).