如何处理溢出情况是本题的难点,通过举一个例子来表达关键代码处的思路。
如果问题背景变成如下:正确的数字范围是-128~127,待分析字符串是-134和129。
1. 重新设置边界。当待分析字符串为负数时 limit = -128; 当待分析字符串为正数时 limit = -127。(注意待分析字符串为正数时,边界limit被故意定义为了负数)
2. 分析-134时,关键代码第一行中的判断条件 limit / 10 = -12;此时result = 0。
(1)“-” :此时result = 0; 确定是负数
(2)"1”: 0 > -12 ---------> 此时result = 0; 可以添加
(3)"3”: -1 > -12 --------->此时result = -1; 可以添加
(4)"4”: -13 < -12 ---------> 此时result = -13; 不可以添加(数字有n位,若result的前n - 1位 < 边界的前n - 1位,则无论第n位加上什么,都会超出边界)
3. 分析129时,关键代码第一行中的判断条件 limit / 10 = -12;此时result = 0。
(1)"1”: 0 > -12 ---------> 此时result = 0; 可以添加
(2)"2”: -1 > -12 ---------> 此时result = -1; 可以添加
(3)"9”: -12 == -12 && (result乘以十)-120 < (边界) -127 + (第n位) 9 ---------> 此时result = -12; 不可以添加(数字有n位,虽然前n - 1位都是正常的,但加上第n位的数可能超出边界)
public int StrToInt(String str) { if (str == null || str.length() == 0 || str == "+" || str == "-") return 0; int limit = -Integer.MAX_VALUE; int label = 1; int result = 0; for (int i = 0; i < str.length(); i++){ if (i == 0 && ((str.charAt(i) == '-') || str.charAt(i) == '+')){ // 第一位如果是负数,"+"或者"-" if (str.charAt(i) == '-'){ // 若是"-",则改变边界条件(limit)和符号位(label) limit = Integer.MIN_VALUE; label = -1; } continue; }else if (str.charAt(i) >= '0' && str.charAt(i) <= '9'){ // 如果是数字,继续分析 int num = str.charAt(i) - '0'; int temp = result * 10; if (result >= (limit / 10) && temp >= (limit + num) ){ // 关键在这里,见文字分析 result = result * 10 - num; }else { // 数值溢出, 返回0 return 0; } }else { // 如果是非法字符, 返回0 return 0; } } return label > 0 ? -result: result; }