-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path65. Valid Number.cpp
More file actions
45 lines (45 loc) · 1.31 KB
/
65. Valid Number.cpp
File metadata and controls
45 lines (45 loc) · 1.31 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
class Solution {
public:
bool isNumber(string str) {
enum InputType {
INVALID, SPACE, SIGN, DOT, E, DIGIT, LEN
};
int trans[][LEN] = {
{-1, 0, 1, 2, -1, 3},
{-1, -1, -1, 2, -1, 3},
{-1, -1, -1, -1, -1, 4},
{-1, 5, -1, 4, 6, 3},
{-1, 5, -1, -1, 6, 4},
{-1, 5, -1, -1, -1, -1},
{-1, -1, 7, -1, -1, 8},
{-1, -1, -1, -1, -1, 8},
{-1, 5, -1, -1, -1, 8}
};
int state = 0;
int i = 0;
while (i < str.size())
{
char s = str[i];
InputType input;
if (isspace(s)) {
input = SPACE;
} else if (s == '+' || s == '-') {
input = SIGN;
} else if (s == '.') {
input = DOT;
} else if (s == 'e' || s == 'E') {
input = E;
} else if (isdigit(s)) {
input = DIGIT;
} else {
input = INVALID;
}
state = trans[state][input];
if (state == -1) {
return false;
}
i++;
}
return state == 3 || state == 4 || state == 5 || state == 8;
}
};