-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy path065_Valid_Number.py
52 lines (51 loc) · 1.47 KB
/
065_Valid_Number.py
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
class Solution(object):
# def isNumber(self, s):
# """
# :type s: str
# :rtype: bool
# """
# # remove lead and tail space
# s = s.strip()
# try:
# float(s)
# return True
# except:
# if '.' in s or ' ' in s:
# return False
# temp = s.split('e')
# if len(temp) == 2:
# try:
# int(temp[0])
# int(temp[1])
# except:
# return False
# return True
# return False
def isNumber(self, s):
s = s.strip()
ls, pos = len(s), 0
if ls == 0:
return False
if s[pos] == '+' or s[pos] == '-':
pos += 1
isNumeric = False
while pos < ls and s[pos].isdigit():
pos += 1
isNumeric = True
if pos < ls and s[pos] == '.':
pos += 1
while pos < ls and s[pos].isdigit():
pos += 1
isNumeric = True
elif pos < ls and s[pos] == 'e' and isNumeric:
isNumeric = False
pos += 1
if pos < ls and (s[pos] == '+' or s[pos] == '-'):
pos += 1
while pos < ls and s[pos].isdigit():
pos += 1
isNumeric = True
print pos, ls, isNumeric
if pos == ls and isNumeric:
return True
return False