-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
57 lines (42 loc) · 1.27 KB
/
Copy pathSolution.java
File metadata and controls
57 lines (42 loc) · 1.27 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
class Solution {
private static final Set<Character> allowed = Set.of('+', '-', 'e', 'E', '.');
public boolean isNumber(String s) {
boolean seenDigit = false;
boolean seenDecimal = false;
boolean seenE = false;
int n = s.length();
char[] arr = s.toCharArray();
for (int i = 0; i < n; i++) {
char ch = arr[i];
if (!allowed.contains(ch) && !isDigit(ch)) {
return false;
}
if (isDigit(ch)) {
seenDigit = true;
continue;
}
if (ch == '+' || ch == '-') {
if (i > 0 && arr[i-1] != 'e' && arr[i-1] != 'E') {
return false;
}
}
if (ch == '.') {
if (seenDecimal || seenE) {
return false;
}
seenDecimal = true;
}
if (ch == 'e' || ch == 'E') {
if (seenE || !seenDigit) {
return false;
}
seenE = true;
seenDigit = false;
}
}
return seenDigit;
}
private static boolean isDigit(char ch) {
return ch >= '0' && ch <= '9';
}
}