这个题其实并不难,就是要把各种情况考虑清楚之后就可以了,有几种特殊情况我一开始没考虑到,就是如果字符串开头有空格,那么就先要消空格,还有一种是如果字符串并不全是数字组成,那么就截取最前面的数字进行转化,这一点也没想到。其他的倒是都想到了。具体代码如下:

  

  1 public int atoi(String str) {
  2         //首先str不能是null或者空字符串,否则返回0
  3         if(str==null||str.length()==0){
  4             return 0;
  5         }
  6         
  7         str = str.trim();
  8         
  9         
 10         //其次如果str不是由一系列的0~9组成的,并且中间没有任何其他字符,否则返回最前面的满足条件的数字
 11         if(!str.matches("[+|-]{0,1}[0-9]+")){
 12             boolean firstFind = false;
 13             if(str.charAt(0)=='-'||str.charAt(0)=='+'){
 14                 for(int i=1;i<str.length();i++){
 15                     if(str.charAt(i)>='0'&&str.charAt(i)<='9'){
 16                         continue;
 17                     }else{
 18                         str = str.substring(0, i);
 19                         break;
 20                     }
 21                 }
 22             }else{
 23                 for(int i=0;i<str.length();i++){
 24                     if(str.charAt(i)>='0'&&str.charAt(i)<='9'){
 25                         continue;
 26                     }else{
 27                         str = str.substring(0, i);
 28                         break;
 29                     }
 30                 }
 31             }
 32         }        
 33         
 34         //如果经过处理之后的str长度为0,或者只剩+,-号了,返回0
 35         if((str.length()==1&&(str.charAt(0)=='+'||str.charAt(0)=='-'))||str.length()==0){
 36             return 0;
 37         }
 38         
 39         int maxLen = 10;
 40         boolean findFirstNonZero = false;
 41         boolean negative = false;
 42         if(str.charAt(0)=='-'){
 43             negative = true;
 44             str = str.substring(1);
 45         }else if(str.charAt(0)=='+'){
 46             str = str.substring(1);
 47         }
 48         for(int i=0;i<str.length();i++){
 49             if(!findFirstNonZero&&str.charAt(i)=='0'){
 50                 continue;
 51             }else if(!findFirstNonZero&&str.charAt(i)!='0'){
 52                 findFirstNonZero = true;
 53                 str = str.substring(i);
 54                 break;
 55             }
 56         }
 57         //如果全是0,那么直接返回0就行了
 58         if(!findFirstNonZero){
 59             return 0;
 60         }
 61         
 62         if(negative){
 63             str  = "-"+str;
 64         }
 65         
 66         //如果是正数,除开开头为0,长度仍大于10,肯定溢出了,返回最大值
 67         if(str.charAt(0)!='-'&&str.length()>maxLen){
 68             return Integer.MAX_VALUE;
 69         }
 70         
 71         //如果是负数,除开开头为0,长度仍大于11,肯定溢出,返回最小值
 72         if(str.charAt(0)=='-'&&str.length()>maxLen+1){
 73             return Integer.MIN_VALUE;
 74         }
 75         
 76         //除了长度大于之外,有可能长度正好等于10或者11,但是其中的数字超了,这也需要查看,就是很长一串&&语句
 77         if(str.charAt(0)!='-'&&str.length()==10&&!isBiggerThanMax(str)){
 78             return Integer.MAX_VALUE;
 79         }
 80         if(str.charAt(0)=='-'&&str.length()==11&&!isSmallerThanMin(str)){
 81             return Integer.MIN_VALUE;
 82         }
 83         
 84         int res = 0;
 85         //上面已经排除完了所有的边界情况,接下里的数字都是能够成功转化为数字的,直接逐位遍历,乘10累加
 86         if(negative){
 87             for(int i=1;i<str.length();i++){
 88                 res*=10;
 89                 res+= str.charAt(i)-'0';
 90             }
 91             res = -res;
 92         }else{
 93             for(int i=0;i<str.length();i++){
 94                 res*=10;
 95                 res+= str.charAt(i)-'0';
 96             }
 97         }
 98         
 99         return res;
100         
101     }
102     
103     private boolean isBiggerThanMax(String str){
104         if(str.charAt(0)-'0'<2){
105             return true;
106         }else if(str.charAt(0)-'0'==2){
107             if(str.charAt(1)-'0'<1){
108                 return true;
109             }else if(str.charAt(1)-'0'==1){
110                 if(str.charAt(2)-'0'<4){
111                     return true;
112                 }else if(str.charAt(2)-'0'==4){
113                     if(str.charAt(3)-'0'<7){
114                         return true;
115                     }else if(str.charAt(3)-'0'==7){
116                         if(str.charAt(4)-'0'<4){
117                             return true;
118                         }else if(str.charAt(4)-'0'==4){
119                             if(str.charAt(5)-'0'<8){
120                                 return true;
121                             }else if(str.charAt(5)-'0'==8){
122                                 if(str.charAt(6)-'0'<3){
123                                     return true;
124                                 }else if(str.charAt(6)-'0'==3){
125                                     if(str.charAt(7)-'0'<6){
126                                         return true;
127                                     }else if(str.charAt(7)-'0'==6){
128                                         if(str.charAt(8)-'0'<4){
129                                             return true;
130                                         }else if(str.charAt(8)-'0'==4){
131                                             if(str.charAt(9)-'0'<=7){
132                                                 return true;
133                                             }
134                                         }
135                                     }
136                                 }
137                             }
138                         }
139                     }
140                 }
141             }
142         }
143         
144         
145         return false;
146     }
147     
148     
149     private boolean isSmallerThanMin(String str){
150         if(str.charAt(1)-'0'<2){
151             return true;
152         }else if(str.charAt(1)-'0'==2){
153             if(str.charAt(2)-'0'<1){
154                 return true;
155             }else if(str.charAt(2)-'0'==1){
156                 if(str.charAt(3)-'0'<4){
157                     return true;
158                 }else if(str.charAt(3)-'0'==4){
159                     if(str.charAt(4)-'0'<7){
160                         return true;
161                     }else if(str.charAt(4)-'0'==7){
162                         if(str.charAt(5)-'0'<4){
163                             return true;
164                         }else if(str.charAt(5)-'0'==4){
165                             if(str.charAt(6)-'0'<8){
166                                 return true;
167                             }else if(str.charAt(6)-'0'==8){
168                                 if(str.charAt(7)-'0'<3){
169                                     return true;
170                                 }else if(str.charAt(7)-'0'==3){
171                                     if(str.charAt(8)-'0'<6){
172                                         return true;
173                                     }else if(str.charAt(8)-'0'==6){
174                                         if(str.charAt(9)-'0'<4){
175                                             return true;
176                                         }else if(str.charAt(9)-'0'==4){
177                                             if(str.charAt(10)-'0'<=8){
178                                                 return true;
179                                             }
180                                         }
181                                     }
182                                 }
183                             }
184                         }
185                     }
186                 }
187             }
188         }
189         return false;
190     }
191