[Leetcode] 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 (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 组合之和的更多相关文章
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- LeetCode OJ:Combination Sum II (组合之和 II)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- java中的比较:instanceof、equals(hashcode)、==
import javassist.expr.Instanceof; class Person{ String s; Person(String s){ this.s=s; } } class Man ...
- WebDriver--定位元素的8种方式
在UI层面的自动化测试开发中,元素的定位与操作是基础,也是经常遇到的困难所在.webdriver提供了8种定位: 1. id定位:find_element_by_id("id值") ...
- Selenium(Python)调用pywin32上传图片
import unittestfrom time import sleep import osfrom selenium import webdriverimport win32apiimport w ...
- 移动性能测试之gemebench安装
越来越多的人从事各种移动端性能测试,但工具和文档的资料却相对较少,这两天需要测试一款APP的性能,就来先简单介绍下gamebench的安装吧! 作为国人来说,使用gamebench还是有相当多的坑点: ...
- jmeter 函数助手
1.选项,函数助手对话框,打开函数助手 2.使用方法 输入参数,点击生成,可以直接使用(Name of variable in which to store the result (optional) ...
- Ubuntu卡在logo界面
对于这个问题,我也是在最近一次偶然的机会中发现的. 我重装了了Ubuntu 18.04, 很多东西需要重新配置, 有个刚性需求就是配置shadowsocks实现***,对于从windows向linu ...
- 【button】 按钮组件说明
原型: <button size="[default | mini]" type="[primary | default | warn]" plain=& ...
- 【第五章】MySQL数据库的安全机制
MySQL权限表MySQL用户管理MySQL权限管理SSL加密连接
- Windows下PHP安全环境的搭建
笔者一直在Windows环境下搭建PHP的运行环境,大大小小的运行环境用过不少,从开始的WAMP到后来的XAMPP以及PHPnow.WAMP和XAMPP都是继承mysql apache以及PHP库的运 ...
- Python3 Tkinter-Pack
1.创建 from tkinter import * root=Tk() print(root.pack_slaves()) Label(root,text='pack').pack() print( ...