forked from rpj911/LeetCode_algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsNumber.java
More file actions
107 lines (91 loc) · 2.84 KB
/
IsNumber.java
File metadata and controls
107 lines (91 loc) · 2.84 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package Algorithms.string;
public class IsNumber {
public static void main(String[] strs) {
System.out.println(isNumber("e"));
}
public boolean isNumber1(String s) {
if (s == null) {
return false;
}
// cut the leading spaces and tail spaces.
String sCut = s.trim();
/*
Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
*/
int len = sCut.length();
boolean num = false;
boolean exp = false;
boolean dot = false;
for (int i = 0; i < len; i++) {
char c = sCut.charAt(i);
if (c == 'e') {
if (!num || exp) {
return false;
}
exp = true;
num = false; // Should be: 2e2 , so there should be number follow "e"
} else if (c <= '9' && c >= '0') {
num = true;
} else if (c == '.') {
if (exp || dot) { // can't be: e0.2 can't be: ..
return false;
}
dot = true;
} else if (c == '+' || c == '-') {
if (i != 0 && sCut.charAt(i - 1) != 'e') { // filter : " 005047e+6", this is true.
return false;
}
} else {
// invalid character.
return false;
}
}
return num;
}
public boolean isNumber(String s) {
if (s == null) {
return false;
}
boolean exp = false;
boolean num = false;
boolean point = false;
s = s.trim();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c <= '9' && c >= '0') {
num = true;
} else if (c == 'e') {
// "2e10" => true
// "ee10" => false
// e10 => false;
// 1.0e => false;
if (exp || !num) {
return false;
}
// reset the num.because we need number after the exp.
num = false;
exp = true;
} else if (c == '.') {
// .. false.
// e. false;
if (point || exp) {
return false;
}
point = true;
} else if (c == '+' || c == '-') {
// +3e+3 is ok.
if (i != 0 && s.charAt(i - 1) != 'e') {
return false;
}
} else {
return false;
}
}
return num;
}
}