-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_08_StringToInteger.java
More file actions
42 lines (34 loc) · 1.04 KB
/
_08_StringToInteger.java
File metadata and controls
42 lines (34 loc) · 1.04 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
public class _08_StringToInteger {
static int myAtoi(String s) {
int len = s.length();
int i = 0;
int sign = 1;
long result = 0;
// 1. Skip leading spaces
while (i < len && s.charAt(i) == ' ') {
i++;
}
// 2. Check sign
if (i < len && (s.charAt(i) == '-' || s.charAt(i) == '+')) {
sign = (s.charAt(i) == '-') ? -1 : 1;
i++;
}
// 3. Convert digits
while (i < len && Character.isDigit(s.charAt(i))) {
result = result * 10 + (s.charAt(i) - '0');
// 4. Overflow check
if (sign == 1 && result > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
if (sign == -1 && -result < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
i++;
}
return (int)(sign * result);
}
public static void main(String... args) {
int rs = myAtoi(" 45875ahgv");
System.out.println(rs); // 45875
}
}