感觉可以用正则去实现
public static int myAtoi1(String s) {
String replace = "";
Boolean flag = true;
for (char c : s.toCharArray()) {
if (flag && String.valueOf(c).equals(" ")) {
continue;
}
flag = false;
replace += c;
}
int result = 0;
for (int i = 1; i <= replace.length(); i++) {
String substring = replace.substring(0, i);
if (substring.equals("-") || substring.equals("+")) {
continue;
}
try {
long l = Long.parseLong(substring);
if (l > Integer.MAX_VALUE) {
result = Integer.MAX_VALUE;
continue;
}
if (l < Integer.MIN_VALUE) {
result = Integer.MIN_VALUE;
continue;
}
result = (int) l;
} catch (Exception e) {
return result;
}
}
return result;
}