Re: [閒聊] 每日leetcode
看板Marginalman作者DJYOSHITAKA (franchouchouISBEST)時間1年前 (2024/10/20 22:20)推噓1(1推 0噓 0→)留言1則, 1人參與討論串1014/1554 (看更多)
直接看別人寫的
這種題目我真的爛到懶得響
希望我這樣抄答案能記起來==
def parseBoolExpr(self, expression: str) -> bool:
stk = []
for c in expression:
if c in ["(", ","]:
continue
elif c in ["t","f", "|", "&", "!"]:
stk.append(c)
elif c == ")":
isTrue = False
isFalse = False
while stk[-1] in ["t", "f"]:
c = stk.pop()
if c == "t":
isTrue = True
else:
isFalse = True
operator = stk.pop()
if operator == "|":
stk.append("t" if isTrue else "f")
elif operator == "&":
stk.append("f" if isFalse else "t")
elif operator == "!":
stk.append("t" if isFalse else "f")
return True if stk[0]=="t" else False
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.229.37.69 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1729434052.A.73E.html
推
10/20 22:24,
1年前
, 1F
10/20 22:24, 1F
討論串 (同標題文章)
完整討論串 (本文為第 1014 之 1554 篇):