-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path241.py
31 lines (29 loc) · 977 Bytes
/
241.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
#!/usr/bin/env python
# coding=utf-8
class Solution(object):
def diffWaysToCompute(self, input):
"""
:type input: str
:rtype: List[int]
"""
ret = []
for i in xrange(len(input)):
if input[i] == '+' or input[i] == '-' or input[i] == '*':
part1Ret = self.diffWaysToCompute(input[:i])
part2Ret = self.diffWaysToCompute(input[i+1:])
for v1 in part1Ret:
for v2 in part2Ret:
c = 0
if input[i] == '+':
c = v1+v2
elif input[i] == '-':
c = v1-v2
elif input[i] == '*':
c = v1*v2
ret.append(c)
if not len(ret):
ret.append(int(input))
return ret
if __name__ == '__main__':
s = Solution()
s.diffWaysToCompute("2-1-1")