/
Combination Sum II.py
61 lines (55 loc) · 2.02 KB
/
Combination Sum II.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Solution:
def helper(self, candidates, target, start, path, res):
if target == 0:
res.append(path)
return
for i in range(start, len(candidates)):
if target - candidates[start] >= 0:
if i != start and candidates[i] == candidates[i - 1]:
continue
self.helper(candidates, target - candidates[i], i + 1,
path + [candidates[i]], res)
else:
return
# @param {integer[]} candidates
# @param {integer} target
# @return {integer[][]}
def combinationSum2(self, candidates, target):
res = []
self.helper(sorted(candidates), target, 0, [], res)
return res
class Solution:
def helper(self, candidates, target, start, path, res):
if target == 0:
if path not in res: # slow
res.append(path)
return
for i in range(start, len(candidates)):
if target - candidates[start] >= 0:
self.helper(candidates, target - candidates[i], i + 1, path + [candidates[i]], res)
else:
return
# @param {integer[]} candidates
# @param {integer} target
# @return {integer[][]}
def combinationSum2(self, candidates, target):
res = []
self.helper(sorted(candidates), target, 0, [], res)
return res
# TLE
class Solution:
def find(self, candidates, target, res, path, pos):
if target == 0:
res.append(path)
return
if target < 0 or pos >= len(candidates) or candidates[pos] > candidates:
return
self.find(candidates, target, res, path, pos + 1)
self.find(candidates, target - candidates[pos], res, path + [candidates[pos]], \
pos + 1)
# @param {integer[]} candidates
# @param {integer} target
# @return {integer[][]}
def combinationSum2(self, candidates, target):
res = []
self.find(sorted(candidates), target, res, [], 0)