-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonCode.py
More file actions
20 lines (20 loc) · 812 Bytes
/
pythonCode.py
File metadata and controls
20 lines (20 loc) · 812 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
def addOperators(self, num: str, target: int):
def backTrack(index, pre, cur, value, s):
if index == n:
if value == target and cur == 0:
ans.append(s)
return
cur = cur * 10 + int(num[index])
if cur > 0:
backTrack(index+1, pre, cur, value, s)
if not s:
backTrack(index+1, cur, 0, value+cur, str(cur))
else:
backTrack(index+1, cur, 0, value+cur, s+'+'+str(cur))
backTrack(index+1, -cur, 0, value-cur, s+'-'+str(cur))
backTrack(index+1, pre*cur, 0, value-pre+pre*cur, s+'*'+str(cur))
n = len(num)
ans = []
backTrack(0, 0, 0, 0, "")
return ans