-
Notifications
You must be signed in to change notification settings - Fork 0
/
Automaton.py
97 lines (85 loc) · 2.66 KB
/
Automaton.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
94
95
# 65. Valid Number
class Solution:
def isNumber(self, s: str) -> bool:
dfa = [
{"digit": 1, "sign": 2, "dot": 3},
{"digit": 1, "dot": 4, "exponent": 5},
{"digit": 1, "dot": 3},
{"digit": 4},
{"digit": 4, "exponent": 5},
{"sign": 6, "digit": 7},
{"digit": 7},
{"digit": 7}
]
cur = 0
for c in s:
if c.isdigit():
group = "digit"
elif c in "+-":
group = "sign"
elif c in "eE":
group = "exponent"
elif c == '.':
group = "dot"
else:
return False
if group not in dfa[cur]:
return False
cur = dfa[cur][group]
return cur in [1, 4, 7]
class Solution:
def isNumber(self, s: str) -> bool:
seen_digit = seen_exponent = seen_dot = 0
for i,c in enumerate(s):
if c.isdigit():
seen_digit = True
elif c in "+-":
if i>0 and s[i-1] not in 'eE':
return False
elif c in ['e', 'E']:
if seen_exponent or not seen_digit:
return False
seen_exponent = True
seen_digit = False
elif c=='.':
if seen_dot or seen_exponent:
return False
seen_dot = True
else:
return False
return seen_digit
# 8. String to Integer (atoi)
INT_MAX = 2 ** 31 - 1
INT_MIN = -2 ** 31
class Automaton:
def __init__(self):
self.state = 'start'
self.sign = 1
self.ans = 0
self.table = {
'start': ['start', 'signed', 'in_number', 'end'],
'signed': ['end', 'end', 'in_number', 'end'],
'in_number': ['end', 'end', 'in_number', 'end'],
'end': ['end', 'end', 'end', 'end'],
}
def get_col(self, c):
if c.isspace():
return 0
if c == '+' or c == '-':
return 1
if c.isdigit():
return 2
return 3
def get(self, c):
self.state = self.table[self.state][self.get_col(c)]
if self.state == 'in_number':
self.ans = self.ans * 10 + int(c)
self.ans = min(self.ans, INT_MAX) if self.sign == 1 else min(self.ans, -INT_MIN)
elif self.state == 'signed':
self.sign = 1 if c == '+' else -1
class Solution:
def myAtoi(self, str: str) -> int:
automaton = Automaton()
for c in str:
automaton.get(c)
return automaton.sign * automaton.ans