-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatoi.txt
27 lines (23 loc) · 999 Bytes
/
atoi.txt
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
class Solution {
public int myAtoi(String str) {
int i = 0;
int sign = 1;
int result = 0;
if (str.length() == 0) return 0;
//Discard whitespaces in the beginning
while (i < str.length() && str.charAt(i) == ' ')
i++;
// Check if optional sign if it exists
if (i < str.length() && (str.charAt(i) == '+' || str.charAt(i) == '-'))
sign = (str.charAt(i++) == '-') ? -1 : 1;
// Build the result and check for overflow/underflow condition
while (i < str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
if (result > Integer.MAX_VALUE / 10 ||
(result == Integer.MAX_VALUE / 10 && str.charAt(i) - '0' > Integer.MAX_VALUE % 10)) {
return (sign == 1) ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
result = result * 10 + (str.charAt(i++) - '0');
}
return result * sign;
}
}