-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathValidNumber.cs
executable file
·87 lines (83 loc) · 5.31 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Source : https://leetcode.com/problems/valid-number/
// Author : codeyu
// Date : Sunday, December 11, 2016 4:12:06 PM
/**********************************************************************************
*
* 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.
*
*
*
* Update (2015-02-10):
* The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.
*
*
**********************************************************************************/
using System;
using System.Collections.Generic;
using Algorithms.Utils;
namespace Algorithms
{
public class Solution065
{
public static bool IsNumber(string s)
{
if(s==null)
return false;
s = s.Trim();
if(s.Length==0)
return false;
bool dotFlag = false;
bool eFlag = false;
for(int i=0;i<s.Length;i++)
{
switch(s[i])
{
case '.':
if(dotFlag || eFlag
|| ((i==0||!(s[i-1]>='0'&&s[i-1]<='9'))
&& (i==s.Length-1||!(s[i+1]>='0'&&s[i+1]<='9'))))
return false;
dotFlag = true;
break;
case '+':
case '-':
if((i>0 && (s[i-1]!='e' && s[i-1]!='E'))
|| (i==s.Length-1 || !(s[i+1]>='0'&&s[i+1]<='9'||s[i+1]=='.')))
return false;
break;
case 'e':
case 'E':
if(eFlag || i==s.Length-1 || i==0)
return false;
eFlag = true;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
break;
default:
return false;
}
}
return true;
}
}
}