
class Solution:
def parseBoolExpr(self, expression: str) -> bool:
st = []
for c in expression:
if c == ',':
continue
if c != ')':
st.append(c)
continue
t = f = 0
while st[-1] != '(':
if st.pop() == 't':
t += 1
else:
f += 1
st.pop()
op = st.pop()
if op == '!':
it = 't' if f == 1 else 'f'
elif op == '&':
it = 't' if f == 0 else 'f'
else:
it = 't' if t > 0 else 'f'
st.append(it)
return st[0] == 't'
栈就是适合计算这种表达式的