纯粹是模拟题目,注意一下要回出现的情况即可
public class Solution {
public int atoi(String str) {
if(str != null){
str = str.trim();
if(str.length() == 0){
return 0;
}
}else{
return 0;
}
char[] chs = str.toCharArray();
int i = 0;
boolean flag = false;
if(chs[i] == '-'){
flag = true;
++i;
}else if(chs[i] == '+'){
++i;
}
long ans = 0;
for(; i < chs.length; ++i){
if(chs[i] < '0' || chs[i] > '9'){
break;
}
ans = ans * 10 + chs[i] - '0';
}
ans = flag ? -ans : ans;
if(ans > Integer.MAX_VALUE){
ans = Integer.MAX_VALUE;
}
if(ans < Integer.MIN_VALUE){
ans = Integer.MIN_VALUE;
}
return (int)ans;
}
public static void main(String[] args){
Solution s = new Solution();
System.out.println(s.atoi(" 8928921414"));
}
}