-
Notifications
You must be signed in to change notification settings - Fork 0
/
65.valid-number.45094085.ac.cpp
140 lines (134 loc) · 2.77 KB
/
65.valid-number.45094085.ac.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* [65] Valid Number
*
* https://leetcode.com/problems/valid-number
*
* Hard (12.70%)
* Total Accepted:
* Total Submissions:
* Testcase Example: '"3"'
*
* Validate if a given string is numeric.
*
*
* Some examples:
* "0" => true
* " 0.1 " => true
* "abc" => false
* "1 a" => false
* "2e10" => true
*
*
* Note: It is intended for the problem statement to be ambiguous. You should
* gather all requirements up front before implementing one.
*
*
*
* Update (2015-02-10):
* The signature of the C++ function had been updated. If you still see your
* function signature accepts a const char * argument, please click the reload
* button to reset your code definition.
*
*/
class Solution {
static inline void exhaustNumber(string& s, int& start)
{
while(start < s.size() && (s[start] >= '0' && s[start] <= '9'))
{
start++;
}
}
public:
static inline bool stateMachine(string s)
{
int i = 0;
enum state{
INT,
FLOAT,
SCIENCE,
SCIENCE_POSTFIX,
INVALID,
}curr = INT;
int point,origin;
for(;i<s.size();)
{
switch(curr)
{
case INT:
if(s[i] == '+' || s[i] == '-')
{
i++;
if(i == s.size())
{
curr = INVALID;
break;
}
}
exhaustNumber(s,i);
if(i == s.size())break;
else if(s[i] == '.')curr = FLOAT;
//必须是小写e
else if(s[i] == 'e')curr = SCIENCE;
else curr = INVALID;
break;
case FLOAT:
point = i;
i++;
exhaustNumber(s,i);
//判断小数点的两边有没有数字,左边没,且右边没,则无效
if((point == 0 || s[point - 1] == '+' || s[point-1] == '-') && point == i-1)
{
curr = INVALID;
break;
}
if(i == s.size())break;
//必须是小写e
else if(s[i] == 'e')curr = SCIENCE;
else curr = INVALID;
break;
case SCIENCE:
//跳过e
if(i == 0 || s[i - 1] == '+' || s[i - 1] == '-')
{
curr = INVALID;
break;
}
i++;
if(i == s.size())
{
curr = INVALID;
break;
}
curr = SCIENCE_POSTFIX;
if(s[i] == '+' || s[i] == '-')
{
i++;
if(i == s.size())
{
curr = INVALID;
break;
}
}
break;
case SCIENCE_POSTFIX:
origin = i;
exhaustNumber(s,i);
if (i == origin)
{
curr = INVALID;
break;
}
break;
case INVALID:
return false;
}
}
return curr != INVALID;
}
bool isNumber(string s) {
auto begin = s.find_first_not_of(' ');
auto last = s.find_last_not_of(' ') + 1;
if(begin >= last)return false;
return stateMachine(string(s,begin,last-begin));
}
};