-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
42 lines (38 loc) · 957 Bytes
/
Solution.cs
File metadata and controls
42 lines (38 loc) · 957 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
35
36
37
38
39
40
41
42
public class Solution
{
public bool IsNumber(string s)
{
s = s.ToLower();
bool seenDigit = false;
bool seenDot = false;
bool seenE = false;
for (int i = 0; i < s.Length; i++)
{
char c = s[i];
if (char.IsDigit(c))
{
seenDigit = true;
}
else if (c == '+' || c == '-')
{
if (i != 0 && s[i - 1] != 'e') return false;
}
else if (c == '.')
{
if (seenDot || seenE) return false;
seenDot = true;
}
else if (c == 'e')
{
if (seenE || !seenDigit) return false;
seenE = true;
seenDigit = false; // must see digit after e
}
else
{
return false;
}
}
return seenDigit;
}
}