【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 ...
随机推荐
- MyBatis做动态模糊查询时,like后面要不要加单引号??
做项目遇到了个奇怪的问题,项目里面要对商品.账户.进行分别的多条件查询,于是我就采用动态多条件分页查询,起初在做账户部分的时候Mybatis是这样写的 <!-- 动态多条件分页查询 --> ...
- 【WPF学习笔记】之如何设置下拉框读取SqlServer数据库的值:动画系列之(一)
先前条件:设置好数据库,需要三个文件CommandInfo.cs.DbHelperSQL.cs.myHelper.cs,需要修改命名空间,参照之前随笔http://www.cnblogs.com/Ow ...
- JQ动态获取数据
转:JQUERY获取浏览器窗口的高度和宽度 June 27, 2012 <script type="text/javascript"> $(document).read ...
- 图像处理之基础---卷积及其快速算法的C++实现
头文件: /* * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com * * This program is free so ...
- Ubuntu 14.04下单节点Ceph安装(by quqi99)
作者:张华 发表于:2014-06-23版权声明:能够随意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 (http://blog.csdn.net/quqi99 ) Ceph ...
- Grails 简要
一.什么是Grails? Grails is an Open Source, full stack, web application framework for the JVM. It takes a ...
- 微信小程序页面之间的跳转
一.使用标签跳转 index.wxml: 在index.wxml页面添加一个<navigator>元素,在元素里面使用属性url就可以 二. ...
- 九度OJ 1013:开门人和关门人 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5052 解决:2563 题目描述: 每天第一个到机房的人要把门打开,最后一个离开的人要把门关好.现有一堆杂乱的机房签到.签离记录,请 ...
- mooc课程mit6.00.1x--problem set1解决方法
counting vowels: 计算字符串中含有元音字母aeiou的数量 char = 'azcbobobegghakl' num = 0 #利用in方法直接查找字符串char中含有的元音字母数量 ...
- svn 出现冲突时可以使用 meld . 命令合并。 而git的冲突合并详见内容
1.可以在任意目录使用 git mergetool --tool-help 查看 git 所支持的merge tools. 2.可以使用如下配置去设置merge tool 和 diff tool ...