forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0040.py
30 lines (25 loc) · 878 Bytes
/
0040.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
class Solution:
def combinationSum2(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
result = list()
candidates.sort()
self._combinationSum2(candidates, target, 0, list(), result)
return result
def _combinationSum2(self, nums, target, index, path, res):
if target == 0:
res.append(path)
return
if path and target < path[-1]:
return
for i in range(index, len(nums)):
if i > index and nums[i] == nums[i - 1]:
continue
self._combinationSum2(nums, target-nums[i], i + 1, path+[nums[i]], res)
if __name__ == '__main__':
candidates = [10,1,2,7,6,1,5]
target = 8
print(Solution().combinationSum2(candidates, target))