LeetCode 40. Combination Sum II
来源:互联网 发布:黑金手游充值软件 编辑:程序博客网 时间:2024/05/13 06:15
题目:
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
For example, given candidate set [10, 1, 2, 7, 6, 1, 5]
and target 8
,
A solution set is:
[ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6]]题意:
如39题一样,只是不能有重复的组合。
题解:
加一个去重判断
class Solution(object): def combinationSum2(self, candidates, target): """ :type candidates: List[int] :type target: int :rtype: List[List[int]] """ res = [] candidates.sort() self.dfs(candidates, target, 0, [], res) r = [] for item in res: if item not in r: r.append(item) return r def dfs(self, nums, target, index, path, res): if target < 0: #小于0时,回溯 return if target == 0: res.append(path) return for i in range(index, len(nums)): self.dfs(nums, target-nums[i], i+1, path+[nums[i]], res)
0 0
- [LeetCode]40.Combination Sum II
- LeetCode --- 40. Combination Sum II
- LeetCode 40.Combination Sum II
- [Leetcode] 40. Combination Sum II
- LeetCode 40. Combination Sum II
- [leetcode] 40. Combination Sum II
- 40. Combination Sum II LeetCode
- leetcode 40. Combination Sum II
- Leetcode 40. Combination Sum II
- LeetCode 40. Combination Sum II
- leetcode 40. Combination Sum II
- LeetCode 40. Combination Sum II
- LeetCode 40. Combination Sum II
- LeetCode *** 40. Combination Sum II
- 【LeetCode】40. Combination Sum II
- leetcode 40. Combination Sum II
- LeetCode - 40. Combination Sum II
- [LeetCode] 40. Combination Sum II
- 【HDU1171】Big Event in HDU 母函数
- HDU 1874 畅通工程续(Dijkstra算法求最短路径)
- Android Studio解决未识别Java文件(出现红J)问题
- C++11多线程异常
- 第十四周-C语言实验——数组逆序
- LeetCode 40. Combination Sum II
- 802.11(wifi)的MAC层功能
- OpenCV学习笔记——《OpenCV3编程入门》读书笔记
- 自学iOS开发系列----OC(协议和委托代理)
- 主席树
- 史上最详细的Android原生APP中添加ReactNative 进行混合开发教程
- Java解/压/缩多个文件目录
- shell脚本实现俄罗斯方块
- DispatcherServlet工作原理探究