forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0065.cpp
40 lines (39 loc) · 1.07 KB
/
0065.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
#include <string>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
bool isNumber(string s)
{
int i = 0, j = s.size() - 1;
while (s[i] == ' ') ++i;
while (s[j] == ' ') --j;
s = s.substr(i, j - i + 1);
bool e = false, point = false, num = false;
i = 0;
if (s.size() > 0 and (s[i] == '+' or s[i] == '-')) i += 1;
for (;i < s.size(); ++i)
{
char c = s[i];
if (c <= '9' and c >= '0')
{
num = true;
continue;
}
else if (c == 'e' or c == 'E')
{
if (!num or e or i == s.size() - 1) return false;
e = true;
if ((s[i + 1] == '-' or s[i + 1] == '+') and i < s.size() - 2) ++i;
}
else if (c == '.')
{
if (point or e) return false;
point = true;
}
else return false;
}
return num;
}
};