-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path065-ValidNumber.cs
49 lines (44 loc) · 1.5 KB
/
065-ValidNumber.cs
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
//-----------------------------------------------------------------------------
// Runtime: 144ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _065_ValidNumber
{
public bool IsNumber(string s)
{
bool hasPoint = false, hasE = false;
int i = 0;
while (i < s.Length && s[i] == ' ') i++;
int end = s.Length - 1;
while (end >= 0 && s[end] == ' ') end--;
if (i >= s.Length) { return false; }
if (s[i] == '+' || s[i] == '-') i++;
var head = i;
char ch;
for (; i <= end; i++)
{
ch = s[i];
if (ch == '.')
{
if (hasPoint || hasE) { return false; }
if (i == head && i == end) { return false; }
if (i == head && i < end && (s[i + 1] > '9' || s[i + 1] < '0')) { return false; }
hasPoint = true;
}
else if (ch == 'e' || ch == 'E')
{
if (hasE || i == head) { return false; }
if (i < end && (s[i + 1] == '+' || s[i + 1] == '-')) i++;
if (i >= end) { return false; }
hasE = true;
}
else if (ch < '0' || ch > '9')
return false;
}
return true;
}
}
}