leetcode 解题记录 --- 40. Combination Sum II
第一版本代码: class Solution(object): def combinationSum2(self, candidates, target): """ :type candidates: List[int] :type target: int :rtype: List[List[int]] """ candidates.sort() self.ans = [] vis = [0 for i in range(len(candidates))] self.dfs(candidates,[],vis,0,target) return self.ans def dfs(self,nums,tem,vis,start,target): if target==0 and tem not in self.ans: self.ans.append(tem.copy()) return if target<0: return for i in range(start,len(nums)): if nums[i]>target: break if vis[i]==0: tem.append(nums[i]) vis[i] = 1 self.dfs(nums,tem,vis,i,target-nums[i]) tem.pop() vis[i]=0 用vis来判断是否使用