题目描述
解题思路
class Solution {
public int strToInt(String str) {
if (str == null || str.length() == 0) return 0;
char[] strs = str.trim().toCharArray();
if (strs.length == 0) return 0;
int i = 1, sign = 1, res = 0;
int boundary = Integer.MAX_VALUE / 10;
if (strs[0] == '-') {
sign = -1;
} else if (strs[0] != '+') {
i = 0;
}
for (int j = i; j < strs.length; j++) {
if (strs[j] < '0' || strs[j] > '9') break;
int num = strs[j] - '0';
if (res > boundary || (res == boundary && num > 7)) {
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
res = res * 10 + num;
}
return sign * res;
}
}