Re: [闲聊] 每日leetcode

楼主: DJYOSHITAKA (Evans)   2024-09-19 23:14:17
以前写过
完全忘了
今天没什么心情写
直接瞥一下答案

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
作者: Pash97143 (四叶天下第一)   2024-09-19 23:15:00
DJ宝又再偷偷卷了

Links booklink

Contact Us: admin [ a t ] ucptt.com