-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path65. Valid Number.java
More file actions
35 lines (35 loc) · 958 Bytes
/
65. Valid Number.java
File metadata and controls
35 lines (35 loc) · 958 Bytes
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
public class Solution {
public boolean isNumber(String s) {
//state machine
boolean numseen = false;
boolean allowE = true;
boolean point = false;
boolean Eseen = false;
s=s.trim();
for (int i = 0; i < s.length(); i++) {
char tmp = s.charAt(i);
if (tmp == '.') {
if (point || Eseen) {
return false;
}
point = true;
} else if (tmp == 'e') {
if (Eseen || !numseen) {
return false;
}
Eseen = true;
allowE = false;
} else if (tmp >= '0' && tmp <= '9') {
numseen = true;
allowE = true;
} else if (tmp == '+' || tmp == '-') {
if (i > 0 && s.charAt(i - 1) != 'e') {
return false;
}
} else {
return false;
}
}
return allowE && numseen;
}
}