forked from rpj911/LeetCode_algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtoi.java
More file actions
86 lines (70 loc) · 2.43 KB
/
Atoi.java
File metadata and controls
86 lines (70 loc) · 2.43 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package Algorithms.string;
public class Atoi {
public int atoi(String str) {
if (str == null) {
return 0;
}
// remove the spaces in the beginning and the end.
String s = str.trim();
boolean minus = false;
long num = 0;
for (int i = 0; i < s.length(); i++) {
/*
takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
*/
if (i == 0 && s.charAt(i) == '+') {
continue;
} else if (i == 0 && s.charAt(i) == '-'){
// get the
minus = true;
continue;
}
int c = s.charAt(i) - '0';
if (c > 9 || c < 0) {
// invalid character.
break;
}
num = num * 10 + c;
}
// If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is // returned.
if (minus) {
num = -num;
num = Math.max(num, Integer.MIN_VALUE);
} else {
num = Math.min(num, Integer.MAX_VALUE);
}
return (int)num;
}
// SOLUTION 2: the Leetcode test case is improved.
public int atoi2(String str) {
long ret = 0;
// ___+1234__
// Delete the leading and tailing spaces.
String sNew = str.trim();
if (sNew.length() == 0) {
return 0;
}
boolean positive = true;
for (int i = 0; i < sNew.length(); i++) {
char c = sNew.charAt(i);
if (i == 0 && c == '+') {
continue;
} else if (i == 0 && c == '-') {
positive = false;
continue;
}
if (!(c <= '9' && c >= '0')) {
break;
}
int dig = positive ? c - '0': '0' - c;
ret = ret * 10 + dig;
// bug 2: should consider the out of range.
if (ret > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
} else if (ret < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
}
return (int)ret;
}
}