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 ...
随机推荐
- Delphi的自动编译软件Want
Delphi自动编译环境的搭建及使用 什么是Want Want是一套Windows下用于编译Delphi源代码的工具.Want的名称是Windows Ant的意思. Ant是Java下最著名的自动编译 ...
- ZServer4D开源项目
ZServer4D开源项目 ZServer4D 是一套从商业项目剥离而出的云服务器中间件,可以承载百万级的分布式负载服务,并且支持IoT及内网穿透. 作者将它开源了 https://github.co ...
- 解决克隆 centos虚拟机后修改克隆后的机器的ip、mac、uuid失败的问题
解决办法: So here's how we fix it: Remove the kernel's networking interface rules file so that it ca ...
- Android判断用户的网络类型(2/3/4G、wifi)
很多时候需要先判断当前用户的网络,才会继续之后的一些处理逻辑.但网络类型获取这一块,我用我自己的的手机调试时遇到一些问题,这里记录一下. 一加手机一代,移动4G 网络,得到的subtype类型值为17 ...
- FrameLayout和handle实现霓虹灯效果
这个程序的主要思想就是在一个FrameLayout中定义多个TextView,分别设置不同的背景色.因为帧布局的特性,所以这些控件都是叠加起来的.然后,通过定时器循环给handler发送消息,改变控件 ...
- Spring MVC+Mybatis 多数据源配置
文章来自:https://www.jianshu.com/p/fddcc1a6b2d8 1. 继承AbstractRoutingDataSource AbstractRoutingDataSource ...
- 轻松看懂Java字节码
java字节码 计算机只认识0和1.这意味着任何语言编写的程序最终都需要经过编译器编译成机器码才能被计算机执行.所以,我们所编写的程序在不同的平台上运行前都要经过重新编译才能被执行. 而Java刚诞生 ...
- Bluemix结合DevOps Service实现一键部署
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文讲述了怎样通过Bluemix与DevOps Service相结合.来构建与部署一个持 ...
- 小米推送 简介 集成 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- C# 6.0 的那些事
这两天期中考试没时间去看Connect();直播,挺可惜的,考完后补看了Connect(); 把C#6.0的新东西总结一下. 自动属性初始化 (Initializers for auto-proper ...