python leetcode 40. Combination Sum II
阿新 • • 發佈:2018-12-07
與 39. Combination Sum不同的是不能重複取
class Solution(object):
def combinationSum2(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
res=[]
def dfs(cans,index,target,path):
if target==0:
res.append(path)
if target<0:
return
for i in range(index,len(cans)):
if i!=index and cans[i]==cans[i-1]:
continue
dfs(cans,i+1,target-cans[i],path+[cans[i]])
candidates. sort()
dfs(candidates,0,target,[])
return res