-
-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathStringToInteger.java
More file actions
41 lines (31 loc) · 825 Bytes
/
StringToInteger.java
File metadata and controls
41 lines (31 loc) · 825 Bytes
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
package leetcode;
/**
* Created by nikoo28 on 7/19/19 2:39 AM
*/
class StringToInteger {
public int myAtoi(String str) {
str = str.trim();
if (str == null || str.length() == 0)
return 0;
char firstChar = str.charAt(0);
int sign = 1, start = 0, len = str.length();
long sum = 0;
if (firstChar == '+') {
sign = 1;
start++;
} else if (firstChar == '-') {
sign = -1;
start++;
}
for (int i = start; i < len; i++) {
if (!Character.isDigit(str.charAt(i)))
return (int) sum * sign;
sum = sum * 10 + str.charAt(i) - '0';
if (sign == 1 && sum > Integer.MAX_VALUE)
return Integer.MAX_VALUE;
if (sign == -1 && (-1) * sum < Integer.MIN_VALUE)
return Integer.MIN_VALUE;
}
return (int) sum * sign;
}
}