LintCode: Combination Sum
一个数可以使用多次
图:
节点:x(当前的和,当前要考虑的数a[i])
边:x->
y1(当前的和,下一个要考虑的数a[i+1])
y2(当前的和+a[i],下一个要考虑的数a[i+1])
BFS
如何求具体解?
队列里放全部的“部分解”——浪费空间
每个节点存放到它的前一个节点——最终还要计算解
DFS
会好一些
leetcode 77,78,90:枚举全部子集
leetcode 51,52:n皇后问题
class Solution {
public:
void help(vector<int> &a, int now, int sum, int target, vector<int> &path, vector<vector<int> > &ans) {
if (sum > target) {
return ;
}
if (now >= a.size()) {
if (sum == target) {
ans.push_back(path);
}
return ;
}
if ((now == ) || (a[now - ] != a[now])) {
path.push_back(a[now]);
help(a, now, sum + a[now], target, path, ans);
path.pop_back();
}
help(a, now + , sum, target, path, ans);
}
/**
* @param candidates: A list of integers
* @param target:An integer
* @return: A list of lists of integers
*/
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
// write your code here
sort(candidates.begin(), candidates.end());
vector<int> path;
vector<vector<int> > ans;
help(candidates, , , target, path, ans);
return ans;
}
};
LintCode: Combination Sum的更多相关文章
- LintCode: Combination Sum II
C++ DFS class Solution { public: void help(vector<int> &a, int now, int sum, int target, v ...
- [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] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LeetCode:Combination Sum I II
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- Combination Sum | & || & ||| & IV
Combination Sum | Given a set of candidate numbers (C) and a target number (T), find all unique comb ...
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
随机推荐
- 委托、Lambda表达式、事件系列01,委托是什么,委托的基本用法,委托的Method和Target属性
委托是一个类. namespace ConsoleApplication1 { internal delegate void MyDelegate(int val); class Program { ...
- delphi Format格式化函数
Format是一个很常用,却又似乎很烦的方法,本人试图对这个方法的帮助进行一些翻译,让它有一个完整的概貌,以供大家查询之用: 首先看它的声明:function Format(const Format: ...
- 奇怪的问题,疑惑?不用的 User agent 居然gzip不一样?
问题描述: 使用同一款浏览器(Chrome Version 41.0.2272.118 (64-bit)),访问同一个地址:http://www.skhktown.com/hkcity/resourc ...
- iReport数据库连接找不到驱动
- 安全:CSRF
原文地址:http://baike.baidu.com/view/1609487.htm?fr=aladdin. 攻击通过在授权用户访问的页面中包含链接或者脚本的方式工作.例如:一个网站用户Bob可能 ...
- 唐顿庄园第一至五季/全集Downton Abbey迅雷下载
本季Downton Abbey 1(2010)看点:ITV古装剧剧<唐顿庄园>由曾因<高斯福德庄园>而荣获奥斯卡的金牌编剧Julian Fellowes一手打造,明星云集的演员 ...
- 在Android中实现图片的裁剪
本实例的功能是将用户选择的图片裁剪后放入ImagView,布局文件是个Button和ImageView.为了图片的正常显示,我们在裁剪后先将裁剪好的图片先存放到SD卡中,这样就能在以后开启应用 ...
- Asp.net MVC 如何防止CSRF攻击
什么是CSRF攻击? CSRF(Cross-site request forgery跨站请求伪造,也被称成为"one click attack"或者session riding,通 ...
- emouse思·睿—评论与观点整理之五
虽说我主要做的硬件,平时的兴趣爱好比较关注移动互联网,混迹于虎嗅.爱范儿.雷锋网.36Kr.cnBeta.瘾科技.i黑马.TechWeb等这类科技以及创业媒体,遗憾的是系统的去写的并不多,好在还算充分 ...
- 检测Sql Server服务器SQL语句执行情况
1.查找目前SQL Server所执行的SQL语法,并展示资源情况: SQL code ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...