-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path241.Different-Ways-to-Add-Parentheses.py
41 lines (34 loc) · 1.14 KB
/
241.Different-Ways-to-Add-Parentheses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# https://leetcode.com/problems/different-ways-to-add-parentheses/
#
# algorithms
# Hard (48.98%)
# Total Accepted: 69,070
# Total Submissions: 141,016
# beats 100.0% of python submissions
class Solution(object):
def __init__(self):
self.hash_map = {}
def diffWaysToCompute(self, input):
"""
:type input: str
:rtype: List[int]
"""
if input in self.hash_map:
return self.hash_map[input]
left, right, res = [], [], []
for idx, ch in enumerate(input):
if ch == '+' or ch == '-' or ch == '*':
left, right = self.diffWaysToCompute(input[:idx]), self.diffWaysToCompute(input[idx+1:])
for i in left:
for j in right:
if ch == '+':
res += i + j,
elif ch == '-':
res += i - j,
else:
res += i * j,
if len(left) == 0 and len(right) == 0:
return [int(input)]
# res.sort()
self.hash_map[input] = res
return res