Valid Number

Desicription

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.

Solution

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
class Solution {
public:
bool isNumber(string s) {
int index = 0;
for(; s[index] == ' '; index++);
if(s[index] == '+' || s[index] == '-')
index++;
int n_num = 0, n_pt = 0;
for(; ((s[index] <= '9' && s[index] >= '0') || s[index] == '.'); index++)
s[index] == '.'?n_pt++:n_num++;
if(!n_num || n_pt > 1)
return 0;
if(s[index] == 'e'){
index++;
if(s[index] == '+' || s[index] == '-')
index++;
int n_num = 0;
for(; s[index] >= '0' && s[index] <= '9'; index++)
n_num++;
if(!n_num)
return 0;
}
for(; s[index] == ' '; index++);
return !s[index];
}
};