-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path65_ValidNumber.py
executable file
·93 lines (85 loc) · 1.81 KB
/
65_ValidNumber.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
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
87
88
89
90
91
92
93
#! /usr/bin/env python
# -*- coding: utf-8 -*-
class Solution(object):
def isNumber(self, s):
"""DFA
Details can be found here:
https://github.com/xuelangZF/LeetCode/blob/master/Images/65_ValidNumber.png
https://github.com/xuelangZF/LeetCode/blob/master/Images/65_StateConvert.png
"""
s = s.strip()
if not s:
return False
# DFA states change table
DFA_states_change = {
0: {1: 2, 2: 1, 3: 8, 4: -1},
1: {1: 2, 2: -1, 3: 8, 4: -1},
2: {1: 2, 2: -1, 3: 3, 4: 5},
3: {1: 4, 2: -1, 3: -1, 4: 5},
4: {1: 4, 2: -1, 3: -1, 4: 5},
5: {1: 7, 2: 6, 3: -1, 4: -1},
6: {1: 7, 2: -1, 3: -1, 4: -1},
7: {1: 7, 2: -1, 3: -1, 4: -1},
8: {1: 4, 2: -1, 3: -1, 4: -1}
}
current_state = 0
for char in s:
input_num = self.input_num(char)
if not input_num:
return False
next_state = DFA_states_change[current_state][input_num]
if next_state == -1:
return False
current_state = next_state
if (current_state == 2 or current_state == 3 or
current_state == 4 or current_state == 7):
return True
else:
return False
def input_num(self, char):
if char in "0123456789":
return 1
elif char in "+-":
return 2
elif char == ".":
return 3
elif char == "e":
return 4
else:
return 0
# True
"""
" .1"
"012"
"+12"
"-12"
"12e1"
"12e-1"
"12e+1"
"12e0"
"0e1"
"-1e1"
"1.2"
".2"
".1e1"
"+.2"
"1."
" .1 "
"46.e3"
"""
# False
"""
""
".e1"
"+.e3"
"10e1.2"
"+-12"
"12e"
"e1"
"1e1e1"
"0xaf"
" .1 2"
"."
" ."
" -."
"""