Leetcode 40. Combination Sum II
来源:互联网 发布:linux telnet命令 端口 编辑:程序博客网 时间:2024/05/22 04:22
题目
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]]
思路
从第一个往前递归计算
测试用例
[] 3[2] 2[1, 1, 1, 2, 3, 4, 5] 8[2] 0
代码
package leetcodeArray;import java.util.ArrayList;import java.util.Arrays;import java.util.LinkedList;import java.util.List;public class Leetcode40CombinationSum2 {public List<List<Integer>> combinationSum2(int[] candidates, int target) { Arrays.sort(candidates); List<List<Integer>> result = new ArrayList<List<Integer>>(); List<Integer> cur = new LinkedList<Integer>();; combination(candidates, target, result, cur, -1); return result;}private void combination(int[] candidate, int target, List<List<Integer>> result, List<Integer> cur, int start){ if(target == 0){ result.add(new LinkedList<Integer>(cur)); return; } for(int i = start + 1; i < candidate.length && candidate[i] <= target;i++){ cur.add(candidate[i]); combination(candidate, target - candidate[i], result, cur, i); cur.remove(cur.size() - 1); while(i < candidate.length - 1 && candidate[i] == candidate[i+1]) i++; }}}
结果
他山之玉
public List<List<Integer>> combinationSum2(int[] cand, int target) { Arrays.sort(cand); List<List<Integer>> res = new ArrayList<List<Integer>>(); List<Integer> path = new ArrayList<Integer>(); dfs_com(cand, 0, target, path, res); return res;}void dfs_com(int[] cand, int cur, int target, List<Integer> path, List<List<Integer>> res) { if (target == 0) { res.add(new ArrayList(path)); return ; } if (target < 0) return; for (int i = cur; i < cand.length; i++){ if (i > cur && cand[i] == cand[i-1]) continue; path.add(path.size(), cand[i]); dfs_com(cand, i+1, target - cand[i], path, res); path.remove(path.size()-1); }}
class Solution {public: vector<vector<int> > combinationSum2(vector<int> &num, int target) { vector<vector<int>> res; sort(num.begin(),num.end()); vector<int> local; findCombination(res, 0, target, local, num); return res; } void findCombination(vector<vector<int>>& res, const int order, const int target, vector<int>& local, const vector<int>& num) { if(target==0) { res.push_back(local); return; } else { for(int i = order;i<num.size();i++) // iterative component { if(num[i]>target) return; if(i&&num[i]==num[i-1]&&i>order) continue; // check duplicate combination local.push_back(num[i]), findCombination(res,i+1,target-num[i],local,num); // recursive componenet local.pop_back(); } } }};
def combinationSum2(self, candidates, target): candidates.sort() table = [None] + [set() for i in range(target)] for i in candidates: if i > target: break for j in range(target - i, 0, -1): table[i + j] |= {elt + (i,) for elt in table[j]} table[i].add((i,)) return map(list, table[target])
阅读全文
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
- YII框架学习 linux下yii2安装教程
- 来自一个老码农的技术理想
- java中如何实现模糊分页查询
- 【随记】一次测试多个JUnit单元测试类(方法)
- iOS MVC框架模式
- Leetcode 40. Combination Sum II
- 数论模板(总结1)
- mysql-python 安装错误 fatal error C1083: Cannot open include file: 'config-win.h': No such file or direc
- 如何实现模糊查询结果
- Java用正则表达式判断是否为IP
- java中单例模式的三种模式优缺点整理
- Latex网址中出现的波浪号~处理
- Linux命令选项中的21个特殊符号
- 针对高级用户的 Vim 编辑实用技巧