-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path65.有效数字-1.cpp
51 lines (50 loc) · 1.18 KB
/
65.有效数字-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
/*
* @lc app=leetcode.cn id=65 lang=cpp
*
* [65] 有效数字
*/
// 评论区答案
// @lc code=start
class Solution {
public:
bool isNumber(string s) {
if (s.empty()) {
return false;
}
bool seenNum = false;
bool seenE = false;
bool seenDot = false;
for (int i = 0; i < s.size(); i++) {
switch (s[i]) {
case '.':
if (seenE || seenDot) {
return false;
}
seenDot = true;
break;
case 'e':
case 'E':
if (seenE || !seenNum) {
return false;
}
seenE = true;
seenNum = false;
break;
case '+':
case '-':
if (i > 0 && s[i - 1] != 'e' && s[i - 1] != 'E') {
return false;
}
seenNum = false;
break;
default:
if (!isdigit(s[i])) {
return false;
}
seenNum = true;
}
}
return seenNum;
}
};
// @lc code=end