blob: 6395fe08d6da6ff01ded7362a3c4ead3b923be9b (
plain)
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
|
#include "leetcode.h"
class Solution {
public:
bool isNumber(string s) {
int state = 0, flag = 0;
while (s[0] == ' ' || s[s.size() - 1] == ' ')
s.erase(s.size() - 1, 1);
for (int i = 0; i < s.size(); i++) {
if ('0' <= s[i] && s[i] <= '9') {
flag = 1;
if (state <= 2)
state = 2;
else
state = (state <= 5) ? 5 : 7;
} else if (s[i] == '+' || s[i] == '-') {
if (state == 0 || state == 3)
state++;
else
return false;
} else if (s[i] == '.') {
if (state <= 2)
state = 6;
else
return false;
} else if (s[i] == 'e') {
if (flag && (state == 2 || state == 6 || state == 7))
state = 3;
else
return false;
} else
return false;
}
return (state == 2 || state == 5 || (flag && state == 6) || state == 7);
}
};
int main() {
Solution obj;
string s = "3";
if (obj.isNumber(s))
cout << "true";
else
cout << "false";
}
|