-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8. Atoi.java
More file actions
24 lines (24 loc) · 784 Bytes
/
Copy path8. Atoi.java
File metadata and controls
24 lines (24 loc) · 784 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
public class Solution {
public int myAtoi(String str) {
int len = str.length();
if (len == 0) {
return 0;
}
int base = 0;
int sign = 1;
int i = 0;
while (i < len && str.charAt(i++) == ' '){}
if (str.charAt(i) == '+' || str.charAt(i) == '-') {
sign = str.charAt(i) == '+' ? 1 : -1;
}
++i;
while (i < len && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
if (base > Integer.MAX_VALUE / 10 || (base == Integer.MAX_VALUE / 10 && str.charAt(i) >= '7')) {
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
base = base * 10 + str.charAt(i) - '0' ;
i++;
}
return sign * base;
}
}