题目描述
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d19b7ab4f3436717f7b29f8db423259d.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c0979036228c171df57ea7e1e2b4ff7b.png)
解题思路
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9ed76f764d23a5770b7d89dc59f48260.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/28f59b0abc862826d4707369f0d1b582.png)
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;
}
}