forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1106.py
38 lines (34 loc) · 925 Bytes
/
1106.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
import operator
class Solution:
def parseBoolExpr(self, s: str) -> bool:
u = 0
def f1(op):
nonlocal u
u += 2
res = op(True if op == operator.__and__ else False, f())
while s[u] != ')':
u += 1
res = op(res, f())
u += 1
return res
def f2():
nonlocal u
u += 2
res = not f()
u += 1
return res
def f():
nonlocal u
if s[u] == 't':
u += 1
return True
elif s[u] == 'f':
u += 1
return False
elif s[u] == '&':
return f1(operator.__and__)
elif s[u] == '|':
return f1(operator.__or__)
else:
return f2()
return f()