# This is my AC code, There must be some place should be improved, Thanks for pointing them out

• public class Solution {
public int atoi(String str) {

``````    Boolean enter = false;
Boolean positive = true;
int begin = -1;
int end = -1;
int result = 0;
int carry = 1;
int strLength = str.length();
if(strLength==0)return 0;
for(int i=0; i<strLength; i++){
char tempChar = str.charAt(i);
if( tempChar<='9' && tempChar>='0'){
if(!enter){
enter = true;
begin = i;
}
}else{
if(enter){
enter = false;
end = i-1;
break;
}
}
}

if(enter){
enter = false;
end = strLength-1;
}else{
if(begin == -1)
return 0;
}

if(begin-1>=0 && str.charAt(begin-1)=='-'){
positive = false;
}else if(begin-1>=0 && str.charAt(begin-1)=='+'){
positive = true;
}else if(begin-1>=0 && str.charAt(begin-1)==' '){
positive = true;
}else if(begin-1>=0){
return 0;
}

for(int i=0; i<begin-1; i++){
if(str.charAt(i)!=' ')
return 0;
}

if(end-begin+1>10){
if(positive)
return Integer.MAX_VALUE;
else
return Integer.MIN_VALUE;
}

for(int i=end; i>=begin; i--){
int temp = 0;

temp = result + (str.charAt(i)-'0')*carry;

if(result > temp){
if(positive)
return Integer.MAX_VALUE;
else
return Integer.MIN_VALUE;
}else
result = temp;
carry *= 10;
}

if(!positive) return -result;
return result;

}
``````

}

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.