-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplementAtoi.java
59 lines (43 loc) · 1.56 KB
/
ImplementAtoi.java
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
class Solution {
public int myAtoi(String str) {
final int len = str.length();
if (len == 0){
return 0;
}
int index = 0;
// skipping white spaces
while (index < len && str.charAt(index) == ' '){
++index;
}
boolean isNegative = false;
// to handle sign cases
if (index < len) {
if (str.charAt(index) == '-') {
isNegative = true;
++index;
} else if (str.charAt(index) == '+'){
++index;
}
}
int result = 0;
// converting digit(in character form) to integer form
// iterate until non-digit character is not found or we can say iterate till found character is a digit
while (index < len && isDigit(str.charAt(index))) {
/* str.charAt(index) - '0' is to convert the char digit into int digit eg: '5' - '0' --> 5
or else it will store the ASCII value of 5 i.e. 53,
so we do 53(ASCII of 5) - 48(ASCII of 0(zero)) to get 5 as int*/
int digit = str.charAt(index) - '0';
// to avoid integer overflow
if (result > (Integer.MAX_VALUE / 10) || (result == (Integer.MAX_VALUE / 10) && digit > 7)){
return isNegative ? Integer.MIN_VALUE : Integer.MAX_VALUE;
}
// adding digits at their desired place-value
result = (result * 10) + digit;
++index;
}
return isNegative ? -result : result;
}
private boolean isDigit(char ch) {
return ch >= '0' && ch <= '9';
}
}