Combination Sum II
最后更新于:2022-04-02 01:12:31
# Combination Sum II
### Source
- leetcode: [Combination Sum II | LeetCode OJ](https://leetcode.com/problems/combination-sum-ii/)
- lintcode: [(153) Combination Sum II](http://www.lintcode.com/en/problem/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.
Have you met this question in a real interview? Yes
Example
For example, given candidate set 10,1,6,7,2,1,5 and target 8,
A solution set is:
[1,7]
[1,2,5]
[2,6]
[1,1,6]
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.
~~~
### 题解
和 [Unique Subsets](http://algorithm.yuanbin.me/zh-cn/exhaustive_search/unique_subsets.html) 非常类似。在 [Combination Sum](http://algorithm.yuanbin.me/zh-cn/exhaustive_search/combination_sum.html) 的基础上改改就好了。
### Java
~~~
public class Solution {
/**
* @param num: Given the candidate numbers
* @param target: Given the target number
* @return: All the combinations that sum to target
*/
public List
';
- > combinationSum2(int[] num, int target) {
List
- > result = new ArrayList
- >();
List
- > result) {
if (gap == 0) {
result.add(new ArrayList