-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyAtoi.java
More file actions
60 lines (55 loc) · 1.81 KB
/
MyAtoi.java
File metadata and controls
60 lines (55 loc) · 1.81 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
/**
* Implement atoi which converts a string to an integer.
* @author LBW
*/
public class MyAtoi {
public int myAtoi(String str) {
int len = str.length();
//judge whether str is empty.
if (len <= 0)
return 0;
StringBuilder stringBuilder = new StringBuilder(str);
boolean negative = false; //whether the integer is positive or negative.
int limit = -Integer.MAX_VALUE;
int result = 0;
int i = 0;
//remove whitespace characters.
while (stringBuilder.length() > 0 && stringBuilder.charAt(0) == ' ')
stringBuilder.deleteCharAt(0);
len = stringBuilder.length();
if (len <= 0) //judge whether str is all whitespace
return 0;
//judge plus-minus sign.
char c = stringBuilder.charAt(0);
if (c < '0') {
if (c == '-') {
negative = true;
limit = Integer.MIN_VALUE;
}
else if (c != '+')
return 0;
if (len == 1) //Cannot have lone "+" or "-".
return 0;
i++;
}
//accumulate the result
for(; i < len; i++) {
c = stringBuilder.charAt(i);
if (Character.digit(c, 10) >= 0) {
//judge whether the value is going to overflow
if (result < (limit + Character.digit(c, 10)) / 10) {
return negative ? limit : -limit;
}
result = result * 10 - Character.digit(c, 10);
}
else
break;
}
result = negative ? result : -result;
return result;
}
public static void main(String[] args) {
MyAtoi myAtoi = new MyAtoi();
System.out.println(myAtoi.myAtoi("-9999999999"));
}
}