-
Notifications
You must be signed in to change notification settings - Fork 2
/
StringToInteger.java
69 lines (58 loc) · 1.87 KB
/
StringToInteger.java
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
package leetcode.string;
public class StringToInteger {
public int myAtoi(String str) {
if (str == null || str.length() == 0) {
return 0;
}
// Removes the whitespaces ahead.
int startIndex = 0;
while (str.charAt(startIndex) == 32) {
startIndex++;
// Returns 0 when the whole string is whitespaces.
if (startIndex >= str.length()) {
return 0;
}
}
// Finds the sign of the number and starting of the numeric part.
int sign = 0;
if (str.charAt(startIndex) == 45) {
sign = -1;
startIndex++;
// Returns 0 when the whole string only has a minus sign.
if (startIndex >= str.length()) {
return 0;
}
} else if (str.charAt(startIndex) == 43) {
sign = 1;
startIndex++;
// Returns 0 when the whole string only has a plus sign.
if (startIndex >= str.length()) {
return 0;
}
} else if (isNumeric(str.charAt(startIndex))) {
sign = 1;
} else {
return 0;
}
// Iterates until meeting the first non-numeric character.
long result = 0;
for (int i = startIndex; i < str.length(); i++) {
if (!isNumeric(str.charAt(i))) {
break;
}
result = result * 10 + toNumber(str.charAt(i));
if (sign * result < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
} else if (sign * result > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
}
return (int) result * sign;
}
private boolean isNumeric(char c) {
return c>= 48 && c <= 57;
}
private int toNumber(char c) {
return c - 48;
}
}