LintCode: Combination Sum II
C++
DFS
class Solution {
public:
void help(vector<int> &a, int now, int sum, int target, vector<int> &path, vector<vector<int> > &ans, bool last) {
if (sum > target) {
return ;
}
if (now >= a.size()) {
if (sum == target) {
ans.push_back(path);
}
return ;
}
if ((now == ) || (a[now - ] != a[now]) || last) {
path.push_back(a[now]);
help(a, now + , sum + a[now], target, path, ans, true);
path.pop_back();
}
help(a, now + , sum, target, path, ans, false);
}
/**
* @param num: Given the candidate numbers
* @param target: Given the target number
* @return: All the combinations that sum to target
*/
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
// write your code here
sort(num.begin(), num.end());
vector<int> path;
vector<vector<int> > ans;
help(num, , , target, path, ans, true);
return ans;
}
};
LintCode: Combination Sum II的更多相关文章
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- Combination Sum,Combination Sum II,Combination Sum III
39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- Lintcode: k Sum II
Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where the ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 【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: Combination Sum II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings
1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...
随机推荐
- 什么是.Net, IL, CLI, BCL, FCL, CTS, CLS, CLR, JIT
什么是.NET? 起源:比尔盖茨在2000年的Professional Developers Conference介绍了一个崭新的平台叫作Next Generation Windows Service ...
- javascript 原型继承
因为javascript没有专门的机制去实现类,所以这里只能是借助它的函数能够嵌套的机制来模拟实现类.在javascript中,一个函数,可以包含变量,也可以包含其它的函数,那么,这样子的话,我们就可 ...
- mqtt介绍
MQTT是轻量级基于代理的发布/订阅的消息传输协议,它可以通过很少的代码和带宽和远程设备连接.例如通过卫星和代理连接,通过拨号和医疗保健提供者连接,以及在一些自动化或小型设备上,而且由于小巧,省电,协 ...
- YUI-compressor 在Linux下安装和使用
介绍一个非常流行的javascript压缩工具YUI compressor,可以提供更好的压缩效率:该工具由著名的Yahoo Exceptional Performance项目组出品. JSMin非常 ...
- Java 导出 CSV
package com.cib.cap4j.cfn.util; import java.io.BufferedWriter; import java.io.File; import java.io.F ...
- 【mybatis】【mysql】mybatis查询mysql,group by分组查询报错:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column
mybatis查询mysql,group by分组查询报错:Expression #1 of SELECT list is not in GROUP BY clause and contains no ...
- VisualStudio:添加现有项时使用添加为链接
这个特性很容易忘记使用(很多人可能还不知道),这里解释一下. 添加为链接是指:将指定的文件作为链接添加到项目中,这个文件在作用上和一般的文件没有区别,这样做的好处是可以多个项目共享一个文件,如:连接字 ...
- java.io.IOException: Attempted read from closed stream解决
在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决 原因是EntityUti ...
- centOS配置国内镜像
本文以163为例, cd /etc/yum.repos.d/wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
- EditText中文文档
感谢农民伯伯的翻译文:http://www.cnblogs.com/over140/archive/2010/09/02/1815439.html 属性名称 描述 android:autoLink 设 ...