forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValid Number.java
More file actions
90 lines (81 loc) · 2.22 KB
/
Valid Number.java
File metadata and controls
90 lines (81 loc) · 2.22 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
class Solution {
public boolean isNumber(String s) {
s = s.trim();
char[] ch = s.toCharArray();
int idx = 0;
int n = ch.length;
boolean signFound = false;
boolean decimalFound = false;
boolean numFound = false;
boolean expoFound = false;
while (idx < n) {
if (ch[idx] == '+' || ch[idx] == '-') {
if (numFound || signFound || decimalFound) {
return false;
}
signFound = true;
}
else if (ch[idx] == '.') {
if (decimalFound) {
return false;
}
decimalFound = true;
}
else if (Character.isLetter(ch[idx])) {
if (ch[idx] == 'e') {
if (!numFound) {
return false;
}
idx++;
expoFound = true;
break;
}
else {
return false;
}
}
else if (Character.isDigit(ch[idx])) {
numFound = true;
}
else if (ch[idx] == ' ') {
if (numFound || signFound || decimalFound) {
return false;
}
}
idx++;
}
if (!numFound) {
return false;
}
if (expoFound && idx == n) {
return false;
}
signFound = false;
numFound = false;
while (idx < n) {
if (ch[idx] == '.') {
return false;
}
else if (ch[idx] == '+' || ch[idx] == '-') {
if (signFound) {
return false;
}
if (numFound) {
return false;
}
signFound = true;
}
else if (Character.isDigit(ch[idx])) {
numFound = true;
}
else {
return false;
}
idx++;
}
if (expoFound && !numFound) {
return false;
}
return true;
}
}