class Solution(object):
def __init__(self):
self.res_hash=dict()
def diffWaysToCompute(self, input):
"""
:type input: str
:rtype: List[int]
"""
if input.isdigit():return [int(input)]
if input in self.res_hash:
print 'reused result for',input
return self.res_hash[input]
res=[]
for i in range(len(input)):
if input[i] in '+-*':
res1=self.diffWaysToCompute(input[:i])
res2=self.diffWaysToCompute(input[i+1:])
for a in res1:
for b in res2:
res.append(self.helper(a,b,input[i]))
self.res_hash[input]=res
return res
def helper(self,num1,num2,operator):
if operator=='+':
return num1+num2
elif operator=='-':
return num1-num2
else:
return num1*num2
241. Different Ways to Add Parentheses
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門存皂,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人逢艘,你說我怎么就攤上這事旦袋。” “怎么了它改?”我有些...
- 文/不壞的土叔 我叫張陵疤孕,是天一觀的道長。 經(jīng)常有香客問我央拖,道長祭阀,這世上最難降的妖魔是什么截亦? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮柬讨,結(jié)果婚禮上崩瓤,老公的妹妹穿的比我還像新娘。我一直安慰自己踩官,他們只是感情好却桶,可當我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蔗牡,像睡著了一般颖系。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上辩越,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼赏胚!你這毒婦竟也來了访娶?” 一聲冷哼從身側(cè)響起,我...
- 正文 年R本政府宣布,位于F島的核電站舌涨,受9級特大地震影響糯耍,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一温技、第九天 我趴在偏房一處隱蔽的房頂上張望革为。 院中可真熱鬧,春花似錦舵鳞、人聲如沸震檩。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽抛虏。三九已至,卻和暖如春套才,著一層夾襖步出監(jiān)牢的瞬間迂猴,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- Given a string of numbers and operators, return all possi...
- Given a string of numbers and operators, return all possi...
- **Reference: **https://leetcode.com/problems/different-wa...
- Given a string of numbers and operators, return all possi...
- Question description: My code: LeetCode result: Solution:...