forked from selfboot/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path65_ValidNumber.cpp
More file actions
107 lines (102 loc) · 1.87 KB
/
65_ValidNumber.cpp
File metadata and controls
107 lines (102 loc) · 1.87 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* @Author: xuezaigds@gmail.com
* @Last Modified time: 2016-06-30 15:29:11
*/
class Solution {
public:
bool isNumber(string s) {
/*
DFA. Details can be found here:
https://github.com/xuelangZF/LeetCode/blob/master/Images/65_ValidNumber.png
https://github.com/xuelangZF/LeetCode/blob/master/Images/65_StateConvert.png
*/
//delete the prefix whitespace
while(s[0]==' ') s.erase(0,1);
//delete the suffix whitespace
while(s[s.size()-1]==' ') s.erase(s.size()-1, 1);
if(s.size() == 0){
return false;
}
vector<vector<int>> DFA_states_convert = {
{2, 1, 8, -1},
{2, -1, 8, -1},
{2, -1, 3, 5},
{4, -1, -1, 5},
{4, -1, -1, 5},
{7, 6, -1, -1},
{7, -1, -1, -1},
{7, -1, -1, -1},
{4, -1, -1, -1}
};
int state = 0;
for(char c : s){
int num = input_num(c);
if(num==-1){
return false;
}
state = DFA_states_convert[state][num];
if(state==-1){
return false;
}
}
if(state == 2 || state == 3 || state == 4 || state ==7){
return true;
}
return false;
}
private:
int input_num(char c){
if(c>='0' && c<='9'){
return 0;
}
else if(c=='+' || c=='-'){
return 1;
}
else if(c=='.'){
return 2;
}
else if(c=='e'){
return 3;
}
else{
return -1;
}
}
};
/*
# True
"""
" .1"
"012"
"+12"
"-12"
"12e1"
"12e-1"
"12e+1"
"12e0"
"0e1"
"-1e1"
"1.2"
".2"
".1e1"
"+.2"
"1."
" .1 "
"46.e3"
"""
# False
"""
""
".e1"
"+.e3"
"10e1.2"
"+-12"
"12e"
"e1"
"1e1e1"
"0xaf"
" .1 2"
"."
" ."
" -."
*/