Skip to content

Commit 73f7c4b

Browse files
committed
3
1 parent 1eff33d commit 73f7c4b

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

ValidNumber/ValidNumber.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public:
3+
bool isNumber(const char *s) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
7+
while (*s != '\0' && *s == ' ') s += 1;
8+
if (*s == '\0') return false;
9+
10+
const char *e = s + strlen(s) - 1;
11+
while (s <= e && *e == ' ') e -= 1;
12+
13+
bool is_digit = false;
14+
bool has_dot = false;
15+
bool has_exp = false;
16+
17+
if (*s == '+' || *s == '-') s += 1;
18+
19+
while (s <= e) {
20+
if ('0' <= *s && *s <= '9') {
21+
is_digit = true;
22+
}
23+
else if (*s == '.') {
24+
if (has_dot || has_exp)
25+
return false;
26+
has_dot = true;
27+
}
28+
else if (*s == 'e') {
29+
if (!is_digit || has_exp)
30+
return false;
31+
has_exp = true;
32+
is_digit = false;
33+
}
34+
else if (*s == '+' || *s == '-') {
35+
if (*(s-1) != 'e')
36+
return false;
37+
}
38+
else return false;
39+
s += 1;
40+
}
41+
return is_digit;
42+
}
43+
};

0 commit comments

Comments
 (0)