-
Notifications
You must be signed in to change notification settings - Fork 3
/
65.cpp
50 lines (50 loc) · 1.68 KB
/
65.cpp
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
class Solution {
public:
bool isNumber(string s) {
bool signFlag = false;
bool pointFlag = false;
int beforePointCount = 0;
int afterPointCount = 0;
bool eFlag = false;
bool signEFlag = false;
int eIntCount = 0;
for (auto& c : s) {
if (!eFlag && (c == '+' || c == '-')) {
if (beforePointCount || pointFlag) return false;
if (signFlag) return false;
signFlag = true;
}
else if (!eFlag && (c == '.')) {
if (pointFlag) return false;
pointFlag = true;
}
else if (c == 'E' || c == 'e') {
if (eFlag) return false;
eFlag = true;
if (!pointFlag) {
if (!beforePointCount) return false;
}
else {
if (!beforePointCount && !afterPointCount) return false;
}
}
else if (eFlag && (c == '+' || c == '-')) {
if (eIntCount) return false;
if (signEFlag) return false;
signEFlag = true;
}
else if (eFlag && c == '.') return false;
else if (c >= '0' && c <= '9') {
if (!eFlag && !pointFlag) beforePointCount++;
else if (!eFlag && pointFlag) afterPointCount++;
else eIntCount++;
}
else return false;
}
if (!eFlag) {
if (!pointFlag) return beforePointCount != 0;
else return (beforePointCount != 0) || (afterPointCount != 0);
}
return (eIntCount != 0);
}
};