-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeetCode8.java
35 lines (35 loc) · 1.22 KB
/
LeetCode8.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
public class LeetCode8 {
public int myAtoi(String s) {
char[] arr = s.toCharArray();
boolean digitRead = false;
boolean leedingNull = false;
boolean signRead = false;
boolean neg = false;
long result = 0;
for (char c : arr) {
if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) break;
if (c == ' ' && !digitRead && !signRead) continue;
if (c == '0' && !digitRead) leedingNull = true;
if (leedingNull && (c == '-' || c == '+')) break;
if (c == '-' && !digitRead && !signRead) {
signRead = true;
neg = true;
continue;
}
if (c == '+' && !digitRead && !signRead) {
signRead = true;
continue;
}
if (c >= '0' && c <= '9') {
result *= 10;
result += c - '0';
digitRead = true;
}
if (c < '0' || c > '9') break;
}
if (neg) result = -result;
if (result >= Integer.MAX_VALUE) return Integer.MAX_VALUE;
if (result <= Integer.MIN_VALUE) return Integer.MIN_VALUE;
return (int) result;
}
}