首页 > 代码库 > leetcode------String to Integer (atoi)
leetcode------String to Integer (atoi)
标题: | String to Integer (atoi) |
通过率: | 13.8 |
难度: | 简单 |
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.
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.
这个题写的比较快,总体还是比较简单我是通过不停的submit去看有哪些输入集去进行写的,输入集有:
1、“111”
2、“+111”
3、“-111”
4、“ +111”
5、“111a111”
6、“ +111 111”
以上都是我遇到的。。然后就是越界的问题,一定要判断上下界,
然后就是使用string中的charAt方法时返回的数字一定要减48,如果不懂百度下ASCII码表就知道了。0在ASCII上开始是48.
其他注意的地方就是空格的判断,前后空格用trim,中间不能去掉。
这次代码写的比较烂,勉强算是通过了吧,我的代码如下:
1 public class Solution { 2 public int atoi(String str) { 3 boolean flag=false; 4 long y=0; 5 //str=str.replaceAll(" ",""); 6 str=str.trim(); 7 if(str.length()==0) return 0; 8 if(str.charAt(0)==‘-‘){ 9 flag=true;10 for(int i=1;i<str.length();i++){11 if(Character.isDigit(str.charAt(i))){12 y=y*10+str.charAt(i)-48;13 if((-1*y)<Integer.MIN_VALUE){14 return Integer.MIN_VALUE;15 }16 }else17 break;18 }19 }20 else if(str.charAt(0)==‘+‘){21 for(int i=1;i<str.length();i++){22 if(Character.isDigit(str.charAt(i))){23 y=y*10+str.charAt(i)-48;24 if(y>Integer.MAX_VALUE){25 return Integer.MAX_VALUE;26 }27 }else28 break;29 }30 }31 else32 {33 for(int i=0;i<str.length();i++){34 if(Character.isDigit(str.charAt(i))){35 y=y*10+str.charAt(i)-48;36 if(y>Integer.MAX_VALUE){37 return Integer.MAX_VALUE;38 }39 }else40 break;41 }42 }43 if(flag==true)44 return (int) (-1*y);45 else46 return (int)y;47 }48 49 50 51 }52
然后我去网上查看了大牛们的代码,也放在这里作为参考:
1 public int atoi(String str) { 2 // Start typing your Java solution below 3 // DO NOT write main() function 4 if(str.length() == 0) 5 return 0; 6 int i=0; 7 int flag = 1; 8 long tmp = 0; 9 while(str.charAt(i)==‘ ‘)10 i++;11 if(str.charAt(i) == ‘+‘)12 i++;13 if(str.charAt(i) == ‘-‘)14 {15 i++;16 flag = -1;17 }18 for(int j=i; j<str.length(); j++){19 if(str.charAt(j)>‘9‘ || str.charAt(j)<‘0‘)20 break;21 tmp = (str.charAt(j) - ‘0‘) + tmp * 10;22 }23 tmp = tmp * flag;24 if(tmp>Integer.MAX_VALUE)25 tmp = Integer.MAX_VALUE;26 else if (tmp<Integer.MIN_VALUE)27 tmp = Integer.MIN_VALUE;28 return (int)tmp; 29 }
leetcode------String to Integer (atoi)