public class Solution {
public int myAtoi(String str) {
if (str == null || str.length() == 0) {
return 0;
}
str = str.trim();
boolean isNegative = false;
int pos = 0;
if (str.charAt(0) == '+') {
pos++;
}
if (str.charAt(0) == '-') {
isNegative = true;
pos++;
}
double rst = 0;
while (pos < str.length() && str.charAt(pos) == '0') pos++;
while (pos < str.length()) {
char c = str.charAt(pos);
if (c < '0' || c > '9') {
break;
}
rst = 10 * rst + (c - '0');
pos++;
}
if (isNegative) {
rst = -rst;
}
if (rst > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
if (rst < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
return (int) rst;
}
}