-
Notifications
You must be signed in to change notification settings - Fork 9
/
Valid Number
39 lines (39 loc) · 1.27 KB
/
Valid Number
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
class Solution {
public boolean isNumber(String s) {
if (s == null)return false;
s = s.trim();
if (s.length() == 0) return false;
boolean point = false, sign = false, exponent = false, number = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!Character.isDigit(c)) {
if (c != 'e' && c != '+' && c != '-' && c != '.') return false;
if (c == '.') {
if (point) {
return false;
} else if (exponent) {
return false;
} else {
point = true;
}
}
if (c == 'e') {
if (exponent) return false;
if (!number) return false;
exponent = true;
number = false;
}
if (c == '+' || c == '-') {
if (i == 0) {
sign = true;
} else {
if (s.charAt(i - 1) != 'e') return false;
}
}
} else {
number = true;
}
}
return number;
}
}