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 ...
随机推荐
- 报错:无法截断表 '某表',因为该表正由 FOREIGN KEY 约束引用
某表的某个字段作为另一个表的FOREIGN KEY,在truncate另外一个表后,再truncate某表,就报如上的错. 解决方法: → 删除另外一个表的外键 IF OBJECT_ID(N'[dbo ...
- jQuery把所有被选中的checkbox的某个属性值连接成字符串
有这样的一个需求:对于一组checkbox,当点击每个checkbox后,把当前处于选中状态的checkbox的某个属性值取出来连接成字符串,并以逗号分开. html部分: <input typ ...
- 再谈vc发送键盘、组合键消息
关于向Windows窗口发送Alt组合键的问题,这个真是经典问题啊,在网上找了一下,问的人N多,方法差不多, 但就是没有很好解决问题. 之前找到一个能正确发送的code:(Alt+A) PostMes ...
- iOS: 计算 UIWebView 的内容高度
- (void)webViewDidFinishLoad:(UIWebView *)wb { //方法1 CGFloat documentWidth = [[wb stringByEvaluating ...
- DotNetty 学习
[转载]http://www.cnblogs.com/littlegod/p/7699482.html DotNetty的学习是带着如下这些问题展开: 1. Socket基础框架方案: 通信模式:异步 ...
- 【转】比较init-method,afterPropertiesSet和BeanPostProcessor
一.简单介绍 1.init-method方法,初始化bean的时候执行,可以针对某个具体的bean进行配置.init-method需要在applicationContext.xml配置文档中bean的 ...
- Ioc:The basic pattern for integrating Autofac into your application
The basic pattern for integrating Autofac into your application is: Structure your app with inversio ...
- linux dig命令 转
dig 命令主要用来从 DNS 域名服务器查询主机地址信息. 查询单个域名的 DNS 信息 dig 命令最典型的用法就是查询单个主机的信息. $ dig baidu.com dig 命令默认的输出信息 ...
- 神探夏洛克第一季/全集Sherlock1迅雷下载
第一季 Sherlock Season 1 (2010)看点:夏洛克·福尔摩斯(Sherlock Holmes)是一个虚构的侦探人物,是由19世纪末的英国侦探小说家阿瑟·柯南·道尔所塑造的一个才华横溢 ...
- Android之TextView部分颜色变动
public class StringHandleExampleActivity extends Activity { /** Called when the activity is first cr ...