-
Notifications
You must be signed in to change notification settings - Fork 0
/
Atoi.java
39 lines (39 loc) · 1.17 KB
/
Atoi.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
public class Solution {
// this solution can pass all the test cases
// but it is not good enough, for a better implementation,
// refer to http://arieshout.me/2012/03/implementation-of-atoi.html
public int atoi(String str) {
long result = 0;
int sign = 1;
int i = 0;
int len = str.length();
int intmax = Integer.MAX_VALUE;
long intmin = -(long)Integer.MIN_VALUE;
while (i < len && str.charAt(i) == ' ')
i++;
if (i == len)
return 0;
if (str.charAt(i) == '+')
i++;
else if (str.charAt(i) == '-') {
sign = -1;
i++;
}
char c;
while (i < len) {
c = str.charAt(i);
if (c >= '0' && c <= '9') {
result = result * 10 + (c - '0');
if (sign > 0 && result > intmax)
return Integer.MAX_VALUE;
else if (sign < 0 && result > intmin)
return Integer.MIN_VALUE;
i++;
}
else {
break;
}
}
return sign > 0 ? (int)result : (int)-result;
}
}