class Solution {
public int strToInt(String str) {
if(str==null || str.length()==0){
return 0;
}
long num=0;
int count=1;
char[] ch =str.toCharArray();
for(int i=ch.length-1;i>=0;i--){
//空格
if(ch[i]==' '){
continue ;
}
//正负数
if(ch[i]=='-'){
num=-1*num;
// System.out.println("fushu");
}
//整数前面有字母la123
if(i+1<ch.length){
if(ch[i+1] >='0' && ch[i+1]<='9' ){
if((ch[i]>='A'&& ch[i]<='Z') || (ch[i]>='a' && ch[i]<='z')){
return 0;
}
}
}
//整数后面是字母 123la
if(i-1 >0){
if(ch[i-1]<='0' && ch[i-1]>='9'){
if((ch[i]>='A'&& ch[i]<='Z') || (ch[i]>='a' && ch[i]<='z')){
return 0;
}
}
}
if(ch[i]>='0' && ch[i]<='9'){
// System.out.println(ch[i]);
num+=(ch[i]-'0')*count;
// System.out.println("num ="+num);
count=count*10;
}
}
if(num>Integer.MAX_VALUE ){
return Integer.MAX_VALUE;
}
if(num<Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}
return (int)num;
}
}