forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
86 lines (62 loc) · 1.88 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/// Source : https://leetcode.com/problems/valid-number/
/// Author : liuyubobobo
/// Time : 2021-05-15
#include <iostream>
using namespace std;
/// Simulation
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
bool isNumber(string s) {
if(s.size() == 0) return false;
if(isInteger(s))
return true;
return isEDecimal(s);
}
private:
bool isEDecimal(const string& s){
int epos = s.find('e');
if(epos == string::npos) epos = s.find('E');
if(epos != string::npos){
string a = s.substr(0, epos), b = s.substr(epos + 1);
return isDecimal(a) && isInteger(b);
}
return isDecimal(s);
}
bool isDecimal(const string& s){
if(s.size() == 0) return false;
if(s[0] == '+' || s[0] == '-')
return isNonSignedDecimal(s.substr(1));
return isNonSignedDecimal(s);
}
bool isNonSignedDecimal(const string& s){
int dotpos = s.find('.');
if(dotpos == string::npos) return isInteger(s);
string a = s.substr(0, dotpos), b = s.substr(dotpos + 1);
if(a.size() == 0) return isNonSignedInteger(b);
else if(b.size() == 0) return isInteger(a);
return isInteger(a) && isNonSignedInteger(b);
}
bool isInteger(const string& s){
if(s.size() == 0) return false;
if(s[0] == '+' || s[0] == '-')
return isNonSignedInteger(s.substr(1));
return isNonSignedInteger(s);
}
bool isNonSignedInteger(const string& s){
if(s.size() == 0) return false;
for(char c: s)
if(!isdigit(c)) return false;
return true;
}
};
int main() {
cout << Solution().isNumber(".1") << endl;
// 1
cout << Solution().isNumber("005047e+6") << endl;
// 1
cout << Solution().isNumber(".-4") << endl;
// 0
return 0;
}