-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200108-1.cpp
54 lines (52 loc) · 1.73 KB
/
200108-1.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
// https://leetcode-cn.com/problems/valid-number/
#include <cstdio>
#include <string>
using namespace std;
class Solution {
public:
bool isNumber(string s) {
const char* p = s.c_str();
while (*p == ' ') ++p;
if (*p == '+' || *p == '-') ++p;
bool hasNumber = false;
if (*p != '.') {
if (*p < '0' || *p > '9') return false;
while (*p >= '0' && *p <= '9') { hasNumber = true; ++p; }
}
if (*p == '.') {
++p;
while (*p >= '0' && *p <= '9') { hasNumber = true; ++p; }
}
if (!hasNumber) return false;
if (*p == 'e' || *p == 'E') {
++p;
if (*p == '+' || *p == '-') ++p;
if (*p < '0' || *p > '9') return false;
while (*p >= '0' && *p <= '9') ++p;
}
while (*p == ' ') ++p;
return (!*p);
}
};
int main()
{
Solution s;
printf("%d\n", s.isNumber("0")); // answer: true
printf("%d\n", s.isNumber(" 0.1 ")); // answer: true
printf("%d\n", s.isNumber("abc")); // answer: false
printf("%d\n", s.isNumber("1 a")); // answer: false
printf("%d\n", s.isNumber("2e10")); // answer: true
printf("%d\n", s.isNumber(" -90e3 ")); // answer: true
printf("%d\n", s.isNumber(" 1e")); // answer: false
printf("%d\n", s.isNumber("e3")); // answer: false
printf("%d\n", s.isNumber(" 6e-1")); // answer: true
printf("%d\n", s.isNumber(" 99e2.5 ")); // answer: false
printf("%d\n", s.isNumber("53.5e93")); // answer: true
printf("%d\n", s.isNumber(" --6 ")); // answer: false
printf("%d\n", s.isNumber("-+3")); // answer: false
printf("%d\n", s.isNumber("95a54e53")); // answer: false
printf("%d\n", s.isNumber(".1")); // answer: true
printf("%d\n", s.isNumber("3.")); // answer: true
printf("%d\n", s.isNumber(".")); // answer: false
return 0;
}