Re: [閒聊] 每日leetcode
看板Marginalman作者DJYOSHITAKA (franchouchouISBEST)時間1年前 (2024/09/19 23:14)推噓1(1推 0噓 0→)留言1則, 1人參與討論串882/1548 (看更多)
以前寫過
完全忘了
今天沒什麼心情寫
直接瞥一下答案
哀
def diffWaysToCompute(self, expression: str) -> List[int]:
ans = []
for i,c in enumerate(expression):
if c!='+' and c!='*' and c!='-':
continue
else:
a = self.diffWaysToCompute(expression[:i])
b = self.diffWaysToCompute(expression[i+1:])
for num1 in a:
for num2 in b:
if c=='+':
ans.append(num1+num2)
elif c=='-':
ans.append(num1-num2)
elif c=='*':
ans.append(num1*num2)
if len(ans)==0 and len(expression)>0:
ans.append(int(expression))
return ans
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.229.37.69 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1726758860.A.E62.html
推
09/19 23:15,
1年前
, 1F
09/19 23:15, 1F
討論串 (同標題文章)
完整討論串 (本文為第 882 之 1548 篇):