-
Notifications
You must be signed in to change notification settings - Fork 1
/
Offer20.java
75 lines (71 loc) · 2.11 KB
/
Offer20.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package lcof;
/**
* Offer20. 表示数值的字符串
* 边界条件,各种细节非常多
* @author LBW
*/
public class Offer20 {
public boolean isNumber(String s) {
int n = s.length();
int start = 0;
while (start < n && s.charAt(start) == ' ') {
start += 1;
}
if (start == n) // 全是空格,直接返回false
return false;
int end = n;
while (s.charAt(end - 1) == ' ') {
end -= 1;
}
int indexOfE = start;
while (indexOfE < n && !(s.charAt(indexOfE) == 'e' || s.charAt(indexOfE) == 'E')) {
indexOfE += 1;
}
if (indexOfE < n) { // 说明找到了 e/E
if (indexOfE == n - 1) { // 说明 e/E 后面 没有了
return false;
}
return (isInteger(s, start, indexOfE) || isDecimal(s, start, indexOfE)) && isInteger(s, indexOfE + 1, end);
}
// else
return isInteger(s, start, end) || isDecimal(s, start, end);
}
private boolean isInteger(String s, int start, int end) {
if (s.charAt(start) == '+' || s.charAt(start) == '-') {
start += 1;
}
if (start >= end) {
return false;
}
for (int i = start; i < end; i++) {
if (!Character.isDigit(s.charAt(i))) {
return false;
}
}
return true;
}
private boolean isDecimal(String s, int start, int end) {
if (s.charAt(start) == '+' || s.charAt(start) == '-') {
start += 1;
}
if (start >= end) {
return false;
}
int countOfDot = 0;
for (int i = start; i < end; i++) {
if (s.charAt(i) == '.') {
countOfDot += 1;
if (countOfDot > 1)
return false;
}
else if (!Character.isDigit(s.charAt(i))) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Offer20 offer20 = new Offer20();
offer20.isNumber("0e");
}
}