【Lintcode】153.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.
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]
]
题解:
主要是去重复,多个方法,之前已经介绍过。利用set
Solution 1 ()
class Solution {
public:
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
vector<vector<int> > res;
vector<int> cur;
sort(num.begin(), num.end());
dfs(res, cur, num, target, ); return res;
}
void dfs(vector<vector<int> > &res, vector<int> &cur, vector<int> &num, int target, int pos){
if (!num.empty() && target == ) {
res.push_back(cur);
return;
}
for (int i = pos; i < num.size(); ++i) {
if(i > pos && num[i] == num[i-]) {
continue;
}
if (target - num[i] >= ) {
cur.push_back(num[i]);
dfs(res, cur, num, target - num[i], i + );
cur.pop_back();
} else {
break;
}
}
}
};
【Lintcode】153.Combination Sum II的更多相关文章
- 【LeetCode】40. Combination Sum II (2 solutions)
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 【LeetCode】40. Combination Sum II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...
- 【LeetCode】040. Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 【Lintcode】135.Combination Sum
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
- 【LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- 【LeetCode】167. Two Sum II - Input array is sorted
Difficulty:easy More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...
随机推荐
- RSA非对称算法实现HTTP密码加密传输
目前一般帐号系统,都是https来传输账户性息,申请一个https证书也不贵.但是网站的其它功能并不需要走https协议,https和http混布比较麻烦,所以决定先实现一个http协议传输RSA非对 ...
- dede列表页调用文章,其实是所有页面都可以调用,第一次应用sql标签
{dede:sql sql="SELECT aid,typeid,body,userip FROM `#@__addonarticle` where aid='6' or aid='7' o ...
- spark之pycharm开发设置
方法一: __author__ = 'similarface' import os import sys os.environ['SPARK_HOME']="/Users/similarfa ...
- OS开发之旅之App的生命周期【转载】
原文链接 http://www.360doc.com/content/15/0918/14/27799428_499912639.shtml 在iOS App中,入口函数并不在根目录下,而是在“Sup ...
- iframe子页面获取父页面元素的方法
在iframe子页面获取父页面元素 代码如下: $.('#objld', parent.document); 在父页面获取iframe子页面的元素 代码如下: $("#objid" ...
- linux 经常使用命令
帮助信息 ./configure -help|grep mysql 安装php ./configure --prefix=/usr/local/fastphp --with-mysql=mysqlnd ...
- new 和 make 均是用于分配内存
the-way-to-go_ZH_CN/06.5.md at master · Unknwon/the-way-to-go_ZH_CN https://github.com/Unknwon/the-w ...
- Channel (digital image) 通道 色彩深度 Color_depth
en.wikipedia.org/wiki/Channel_(digital_image) Color digital images are made of pixels, and pixels ar ...
- php总结2——php中的变量、数据类型及转换、运算符、流程控制中的分支结构
2.1 php中的变量: 定义变量:$变量名称=值: 变量名称:$开头 $之后的第一位必须是字母 $第二位之后可以是字母.数字或者是下划线.习惯上变量名称有实际含义,第二个单词后首字母大 ...
- 【题解】Counting D-sets(容斥+欧拉定理)
[题解]Counting D-sets(容斥+欧拉定理) 没时间写先咕咕咕. vjCodeChef - CNTDSETS 就是容斥,只是难了一二三四五\(\dots \inf\)点 题目大意: 给定你 ...