-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseInt.java
More file actions
76 lines (74 loc) · 2.51 KB
/
ParseInt.java
File metadata and controls
76 lines (74 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package leetcode;
/**
* @author JunjunYang
* @date 2019/12/27 9:09
*/
public class ParseInt {
public static void main(String[] args) {
String[] s = {"123", "-12345", "5847"};
for (String str : s) {
System.out.println(Integer.parseInt(str));
System.out.println(parseInt(str, 10));
}
}
/**
* 自行实现parseInt方法
*
* @param str
* @param radix
* @return
*/
public static int parseInt(String str, int radix) {
if (str == null) {
throw new NullPointerException();
}
//验证基数是否合法
if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix is less than " + Character.MIN_RADIX);
}
if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix is larger than " + Character.MIN_RADIX);
}
int i = 0;
int len = str.length();
//此处之所以使用负数作为Limit,是想将范围溢出统一起来,否则正数需要比较最大值,负数需要比较最小值
int limit = -Integer.MAX_VALUE;
int digit;
boolean negative = false;
int result = 0;
if (len > 0) {
char firstChar = str.charAt(0);
//验证首位是否合法
if (firstChar < '0') {
if (firstChar == '-') {
negative = true;
limit = Integer.MIN_VALUE;
} else if (firstChar != '+') {
throw new NumberFormatException();
}
i++;
}
int multiMin = limit / radix;
while (i < len) {
// digit=str.charAt(i++)-48;
digit = Character.digit(str.charAt(i++), radix);
if (digit < 0) {
throw new NumberFormatException();
}
//验证result *= radix是否会溢出
if (result < multiMin) {
throw new NumberFormatException();
}
result *= radix;
//验证result -= digit是否会溢出
if (result < limit + digit) {
throw new NumberFormatException();
}
result -= digit;
}
} else {
throw new NumberFormatException();
}
return negative ? result : -result;
}
}