经典笔试题—字符串转换整数
题目描述
将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。
首先,乍一看题目感觉非常简单, 按length遍历字符串,并在每次循环中,将前面的数值乘10与取到的数进行相加(减)就可以了。其实不然,这道题目不会仅仅考虑这么简单的字符串遍历,更需要在运算中考虑边界值的比较以及非法字符的输入考虑,而在java中,不存在比Integer.MAX_VALUE还大或者比Integer.MIN_VALUE还小的书,所以如果判断输入的字符串溢出就是一个比较有意思的问题。
我在解决问题前,参考了 Integer.parseInt(s)的源码。给我比较好的一个建议。
下面给出parseInt(String s, int radix)的源码。
public static int parseInt(String s, int radix)
throws NumberFormatException
{
if (s == null) {
throw new NumberFormatException("null");
}
if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
}
if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
}
int result = 0;
boolean negative = false;
int i = 0, len = s.length();
int limit = -Integer.MAX_VALUE;
int multmin;
int digit;
if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
negative = true;
limit = Integer.MIN_VALUE;
} else if (firstChar != '+')
throw NumberFormatException.forInputString(s);
if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
multmin = limit / radix;
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
return negative ? result : -result;
}
可以看到,其中在每次的遍历中都会对当先结果以及将要产生的下一个结果进行判断,已确定是否会发生数据溢出,在当前这种思想的借鉴下,我写出了自己的算法以供参考。
public static int parseInt2(String str) {
int x = 0, i = 0;
int sign = 1;
int len = str.length();
char firstChar = str.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
sign = -1;
} else if (firstChar != '+')
throw new NumberFormatException(str);
if (len == 1) // Cannot have lone "+" or "-"
throw new NumberFormatException(str);
i++;
}
int temp = x;
while (i < len) {
if (!(str.charAt(i) >= '0' && str.charAt(i) <= '9')) {
throw new NumberFormatException(str);
}
temp = x;// 记录当前值
switch (sign) {
case -1:
/*关键代码!!!*/
if (temp < Integer.MIN_VALUE / 10
|| temp == Integer.MIN_VALUE / 10
&& Integer.MIN_VALUE % 10 < (int) (str.charAt(i) - '0')) {
throw new NumberFormatException(str);
}
x *= 10;
x = x - (int) (str.charAt(i) - '0');
System.out.println(x);
break;
case 1:
/*关键代码!!!*/
if (temp > Integer.MAX_VALUE / 10
|| temp == Integer.MAX_VALUE / 10
&& Integer.MAX_VALUE % 10 < (int) (str.charAt(i) - '0')) {
throw new NumberFormatException(str);
}
x *= 10;
x = x + (int) (str.charAt(i) - '0');
System.out.println(x);
break;
}
i++;
}
return x;
}
在我的代码中最关键的就是根据当前数据不溢出的情况下,对下一个要产生的结果进行预判。判断不溢出才会进行数据计算,否则抛出异常。