-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path65.cpp
More file actions
34 lines (29 loc) · 979 Bytes
/
65.cpp
File metadata and controls
34 lines (29 loc) · 979 Bytes
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
class Solution {
public:
bool isNumber(string s) {
if(s.length() < 1) return false;
int i = 0; if(s[i] == '+' || s[i] == '-') i++;
int numDots = 0;
bool hasNum = false;
bool hasE = false;
while(i < s.length()){
if(s[i] == '.') {numDots++; i++; continue;}
if(s[i] == 'e' || s[i] == 'E'){hasE = true; i++; break;}
if(s[i]-'0' < 0 || s[i]-'9' > 0) return false;
i++;
if(!hasNum) hasNum = true;
}
if(!hasNum || numDots > 1) return false;
if(hasE){
hasNum = false;
if(i < s.length() && (s[i] == '+' || s[i] == '-')) i++;
while(i < s.length()){
if(s[i]-'0' < 0 || s[i]-'9' > 0) return false;
i++;
if(!hasNum) hasNum = true;
}
if(!hasNum) return false;
}
return true;
}
};