-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path65. Valid Number.cpp
59 lines (58 loc) · 1.46 KB
/
65. Valid Number.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
51
52
53
54
55
56
57
58
59
#include"Inc.h"
class Solution {
public:
bool isNumber(string s) {
int pCount = 0;
int pPosition = 0;
int eCount = 0;
int ePosition = s.size();
int i = 0;
while (s[i] == ' ') i++;
if ((s[i] > '9' || s[i] < '0') && s[i] != '+'&&s[i] != '-'&&s[i] != '.')
return false;
else if (s[i] == '+' || s[i] == '-') i++;
else if (s[i] == '.') {
pPosition = i;
pCount++;
if (i + 1 < s.size())
if (s[i + 1]<'0' || s[i + 1]>'9')
return false;
i++;
}
if (i >= s.size()) return false;
for (; i < s.size(); i++) {
if (s[i] <= '9'&&s[i] >= '0') continue;
else if (s[i] == '.') {
pPosition = i;
if (pCount > 0 || ePosition < pPosition) return false;
else {
pCount++;
if (s[i - 1]<'0' || s[i - 1]>'9') {
if (i == s.size() - 1 || s[i + 1]<'0' || s[i + 1]>'9')
return false;
}
}
}
else if (s[i] == 'e') {
ePosition = i;
if (eCount > 0 || ePosition < pPosition || i == s.size() - 1)
return false;
else {
eCount++;
if ((s[i + 1]<'0' || s[i + 1]>'9') && s[i + 1] != '+'&& s[i + 1] != '-') return false;
if ((s[i - 1]<'0' || s[i - 1]>'9') && s[i - 1] != '.')
return false;
}
}
else if (s[i] == ' ') {
for (int j = i + 1; j < s.size(); j++)
if (s[j] != ' ') return false;
}
else if (s[i] == '+' || s[i] == '-') {
if (s[i - 1] != 'e' || i == s.size() - 1 || (s[i + 1]<'0' || s[i + 1]>'9')) return false;
}
else return false;
}
return true;
}
};