-
Notifications
You must be signed in to change notification settings - Fork 17
/
expression-add-operators.py
44 lines (34 loc) · 1.3 KB
/
expression-add-operators.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
42
43
44
import operator
from typing import List, Set
class Solution:
def addOperators(self, nums: str, target: int) -> List[str]:
stack: List[str] = []
result: Set[str] = set()
def backtrack(pos: int, total: int, last: int) -> None:
if pos == len(nums):
if total == target:
result.add("".join(stack))
return
for size in range(1, len(nums) - pos + 1):
if nums[pos] == "0" and size > 1:
break
num = int(nums[pos : pos + size])
for op, _repr, last_result in [
(operator.add, "+", 0),
(operator.sub, "-", 0),
(operator.mul, "*", last),
]:
diff = op(last_result, num)
stack.append(_repr)
stack.append(str(num))
backtrack(pos + size, total - last_result + diff, diff)
stack.pop()
stack.pop()
for size in range(1, len(nums) + 1):
if nums[0] == "0" and size > 1:
break
num = int(nums[0:size])
stack.append(str(num))
backtrack(size, num, num)
stack.pop()
return list(result)