【LeetCode】40. Combination Sum II (2 solutions)
2014-12-16 15:11
459 查看
Combination Sum II
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.
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set
A solution set is:
解法一:
最直接的想法就是把所有子集都列出来,然后逐个计算和是否为target
但是考虑到空间复杂度,10个数的num数组就有210个子集,因此必须进行“剪枝”,去掉不可能的子集。
先对num进行排序。
在遍历子集的过程中:
(1)单个元素大于target,则后续元素无需扫描了,直接返回结果。
(2)单个子集元素和大于target,则不用加入当前的子集容器了。
(3)单个子集元素和等于target,加入结果数组。
![](http://images.cnitblog.com/blog/458814/201502/230940215641527.jpg)
解法二:递归回溯
需要注意的是:
1、在同一层递归树中,如果某元素已经处理并进入下一层递归,那么与该元素相同的值就应该跳过。否则将出现重复。
例如:1,1,2,3
如果第一个1已经处理并进入下一层递归1,2,3
那么第二个1就应该跳过,因为后续所有情况都已经被覆盖掉。
2、相同元素第一个进入下一层递归,而不是任意一个
例如:1,1,2,3
如果第一个1已经处理并进入下一层递归1,2,3,那么两个1是可以同时成为可行解的
而如果选择的是第二个1并进入下一层递归2,3,那么不会出现两个1的解了。
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.
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set
10,1,2,7,6,1,5and target
8,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
解法一:
最直接的想法就是把所有子集都列出来,然后逐个计算和是否为target
但是考虑到空间复杂度,10个数的num数组就有210个子集,因此必须进行“剪枝”,去掉不可能的子集。
先对num进行排序。
在遍历子集的过程中:
(1)单个元素大于target,则后续元素无需扫描了,直接返回结果。
(2)单个子集元素和大于target,则不用加入当前的子集容器了。
(3)单个子集元素和等于target,加入结果数组。
class Solution { public: vector<vector<int> > combinationSum2(vector<int> &num, int target) { vector<vector<int> > result; vector<vector<int> > subsets; vector<int> empty; subsets.push_back(empty); sort(num.begin(), num.end()); for(int i = 0; i < num.size();) {//for each number int count = 0; int cur = num[i]; if(cur > target) //end return result; while(i < num.size() && num[i] == cur) {//repeat count i ++; count ++; } int size = subsets.size(); //orinigal size instead of calling size() function for(int j = 0; j < size; j ++) { vector<int> sub = subsets[j]; int tempCount = count; while(tempCount --) { sub.push_back(cur); int sum = accumulate(sub.begin(), sub.end(), 0); if(sum == target) { result.push_back(sub); subsets.push_back(sub); } else if(sum < target) subsets.push_back(sub); } } } return result; } };
![](http://images.cnitblog.com/blog/458814/201502/230940215641527.jpg)
解法二:递归回溯
需要注意的是:
1、在同一层递归树中,如果某元素已经处理并进入下一层递归,那么与该元素相同的值就应该跳过。否则将出现重复。
例如:1,1,2,3
如果第一个1已经处理并进入下一层递归1,2,3
那么第二个1就应该跳过,因为后续所有情况都已经被覆盖掉。
2、相同元素第一个进入下一层递归,而不是任意一个
例如:1,1,2,3
如果第一个1已经处理并进入下一层递归1,2,3,那么两个1是可以同时成为可行解的
而如果选择的是第二个1并进入下一层递归2,3,那么不会出现两个1的解了。
class Solution { public: vector<vector<int> > combinationSum2(vector<int> &num, int target) { sort(num.begin(), num.end()); vector<vector<int> > ret; vector<int> cur; Helper(ret, cur, num, target, 0); return ret; } void Helper(vector<vector<int> > &ret, vector<int> cur, vector<int> &num, int target, int position) { if(target == 0) ret.push_back(cur); else { for(int i = position; i < num.size() && num[i] <= target; i ++) { if(i != position && num[i] == num[i-1]) continue; cur.push_back(num[i]); Helper(ret, cur, num, target-num[i], i+1); cur.pop_back(); } } } };
![](http://images.cnitblog.com/blog/458814/201502/230938586272330.jpg)
相关文章推荐
- 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)
- 【LeetCode】96. Unique Binary Search Trees (2 solutions)
- 【LeetCode】12. Integer to Roman (2 solutions)
- [LeetCode] My Solutions in LeetCode Online Judge
- LeetCode Questions List (LeetCode 问题列表)- Java Solutions
- 【LeetCode】115. Populating Next Right Pointers in Each Node (2 solutions)
- 【LeetCode】130. Surrounded Regions (2 solutions)
- 【LeetCode】 Same Tree (2 solutions)
- LeetCode Solutions : Rotate List
- 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)
- 【LeetCode】25. Reverse Nodes in k-Group (2 solutions)
- 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)
- 【LeetCode】207. Course Schedule (2 solutions)
- 【LeetCode】162. Find Peak Element (3 solutions)
- 【LeetCode】142. Linked List Cycle II (2 solutions)
- 【LeetCode】338. Counting Bits (2 solutions)
- 【LeetCode】48. Rotate Image (2 solutions)
- 【LeetCode】206. Reverse Linked List (2 solutions)
- 【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)
- 【LeetCode】75. Sort Colors (3 solutions)