跟上一道题比就改了一个地方,dfs的for循环从下一个开始,上一道是从自己开始dfs
class Solution(object):
ansSet=set()
def combinationSum2(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
if len(candidates)==0:return []
candidates.sort()
self.ansSet.clear()
for i in range(len(candidates)):
#print "fuck"
self.dfs(candidates, i, target, candidates[i], [])
return list(self.ansSet)
def dfs(self, candi, p, target, curV, curAns):
curAns.append(candi[p])
if curV==target:
#newAns=curAns[:]
self.ansSet.add(tuple(curAns))
#print self.ansSet
curAns.pop()
return True
for i in range(p+1,len(candi)):
if curV+candi[i]<=target:
self.dfs(candi, i, target, curV+candi[i], curAns)
else:
curAns.pop()
return False
curAns.pop()
return False