public class Solution {
public boolean isNumChar(char ch) {
return ch >= '0' && ch <= '9';
}
public int StrToInt(String str) {
int le = 0;
if(le >= str.length()) {
return 0;
}
char firstCh = str.charAt(le);
boolean isNegative = false;
if(firstCh == '+') {
le++;
} else if(firstCh == '-') {
le++;
isNegative = true;
} else if(isNumChar(firstCh)) {
} else {
return 0;
}
int ri = le;
while(ri < str.length() && isNumChar(str.charAt(ri))) {
ri++;
}
if(ri != str.length()) {
return 0;
}
if(isNegative) {
long ans = 0;
for(int i = le; i < ri; i++) {
ans = ans * 10 - (str.charAt(i) - '0');
if(ans < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
}
return (int)ans;
} else {
long ans = 0;
for(int i = le; i < ri; i++) {
ans = ans * 10 + (str.charAt(i) - '0');
if(ans > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
}
return (int)ans;
}
}
}