public int myAtoi(String str) {
if(str == null || str.length() == 0){
return 0;
}
//space, + -, non num, out of bound
boolean flag = true;
long result = 0;
str = str.trim();
if(str.charAt(0) == '+'){
str = str.substring(1);
}else if(str.charAt(0) == '-'){
flag = false;
str = str.substring(1);
}
for(int i = 0; i < str.length(); i++){
if(str.charAt(i) >= '0' && str.charAt(i) <= '9'){
result = result * 10 + (str.charAt(i) - '0');
if(result >= Integer.MAX_VALUE){
break;
}
}else{
break;//if there is a non number just stop there
}
}
if(flag == false){
result = -result;
}
if(result <= Integer.MIN_VALUE){//based on requirement
return Integer.MIN_VALUE;
}else if(result >= Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}
return (int)result;
}