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 (a 1, a 2, … , a k) must be in non-descending order. (ie, a 1 ≤ a 2 ≤ … ≤ a k).
  • The solution set must not contain duplicate combinations.

For example, given candidate set10,1,2,7,6,1,5and target8, 
A solution set is: 
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]

题意:给定值T,在C中找到和为T的组合,要求:C中的每个元素只能出现一次,不能用有重复组合。

思路:正确理解,C中的每个元素只能出现一次!如例子中的[1,1,6],只是数组中的每个元素只能出现一次,不是说,元素值相等的只能出现一次。要给出所以满足条件的解,这题是典型的深搜。在深搜之前要注意到题中要求,每个组合中元素的值要是非降序的,所以,要先对数组进行排序。因为数组中元素的值有重复的情况,所以在写DFS函数时,要跳过重复的元素。参考了Grandyang的写法。比如排序以后,题中例子变为[1,1,2,5,6,7,10],解中给出以第一个元素1和7组合的解了,以第二元素1和7的解要跳过。这时也不过跳过由重复数字组成的解,因为,首次出现重复的数字时,已经给出了由重复数字组合等于给定值的情况。

代码如下:

 class Solution {
public:
vector<vector<int> > combinationSum2(vector<int> &num, int target)
{
vector<vector<int>> res;
vector<int> midVal;
sort(num.begin(),num.end());
DFS(res,midVal,num,target,);
return res;
} void DFS(vector<vector<int>> &res,vector<int> &midVal,vector<int> &num,int target,int start)
{
if(target<)
return;
else if(target==)
res.push_back(midVal);
else
{
for(int i=start;i<num.size();i++)
{
if(i>start&&num[i]==num[i-]) //
continue;
midVal.push_back(num[i]);
DFS(res,midVal,num,target-num[i],i+);
midVal.pop_back();
}
}
}
};

[Leetcode] combination sum ii 组合之和的更多相关文章

  1. [LeetCode] Combination Sum II 组合之和之二

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  3. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  4. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  5. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  6. LeetCode OJ:Combination Sum II (组合之和 II)

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  7. [LeetCode] 40. Combination Sum II 组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  8. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  9. [LeetCode] 377. Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. Linux命令应用大词典-第13章 用户和组群管理

    13.1 useradd:创建用户账户 13.2 adduser:创建用户账户 13.3 lnewusers:创建用户账户 13.4 usermod:修改用户账户 13.5 userdel:删除用户账 ...

  2. 第一个线性回归程序(基于Jupyter)

    import pandas as pdimport seaborn as snssns.set(context="notebook", style="whitegrid& ...

  3. LeetCode 144 ——二叉树的前序遍历

    1. 题目 2. 解答 2.1. 递归法 定义一个存放树中数据的向量 data,从根节点开始,如果节点不为空,那么 将当前节点的数值加入到 data 中 递归得到其左子树的数据向量 temp,将 te ...

  4. UVa -1584 Circular Sequence 解题报告 - C语言

    1.题目大意 输入长度为n$(2\le n\le 100)$的环状DNA串,找出该DNA串字典序最小的最小表示. 2.思路 这题特别简单,一一对比不同位置开始的字符串的字典序,更新result. 3. ...

  5. parity 注记词

    spousal tint untold around rosy daintily unrated sheep choice showpiece chirping gala

  6. 预分配内存fifo实现可变长度字节序列存储

    预分配内存fifo实现可变长度字节序列存储 github链接https://github.com/gexin1023/utils/tree/master/fifo fifo即先进先出队列,可以用链表来 ...

  7. [leetcode-783-Minimum Distance Between BST Nodes]

    Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...

  8. php 连接redis查询数据

    class Layoutdemo{ function index(){ $db = new Db(); $id=390; $layout_json = array(); if($info = $db- ...

  9. Python如何运行

    Python是一种解释型语言,在执行Python的时,解释器将源代码source code翻译成字节码byte code,然后byte code交给Python虚拟机PVM去执行,整个流程如下图所示: ...

  10. 20172333 2017-2018-2 《Java程序设计》第10周学习总结

    20172333 2017-2018-2 <Java程序设计>第10周学习总结 教材学习内容 第十三章 集合是一个对象,一个保存其他对象的数据库. 集合可以保存不同种类的对象也可以保存同种 ...