-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCombination-Sum-II.py
31 lines (29 loc) · 993 Bytes
/
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
#! /usr/bin/env python
#! -*- coding=utf-8 -*-
# Date: 2019-12-1
# Author: Bryce
from typing import List
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
res = []
out = []
candidates = sorted(candidates)
self.DFS(candidates,target,0,out,res)
return res
def DFS(self,candidates: List[int], target:int, start: int, out: List[int], res: List[List[int]]):
if target < 0:
return
if target == 0:
res.append(out[:])
return
n = len(candidates)
for i in range(start,n):
if candidates[i] == candidates[i-1] and i > start:
continue
out.append(candidates[i])
self.DFS(candidates,target-candidates[i],i+1,out,res)
del out[-1]
if __name__ == " __main__ ":
candidates = [10,1,2,7,6,1,5]
target = 8
print (Solution().combinationSum2(candidates, target))