combination sum(I, II, III, IV)
II 简单dfs
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) { vector<vector<int>> ans;
vector<int> cur;
sort(candidates.begin(), candidates.end()); // 排序后去重
dfs(,target,candidates,cur,ans);
return ans;
}
void dfs(int level, int target, vector<int>& candidates,vector<int>& cur,vector<vector<int>> &ans)
{
if(target==)
{
ans.push_back(cur);
return;
}
if(target<)return;
for(int i=level;i<candidates.size();i++)
{
if (i > level && candidates[i] == candidates[i - ])continue;// 去重
cur.push_back(candidates[i]);
dfs(i+,target-candidates[i],candidates,cur,ans);
cur.pop_back();
} }
III 简单dfs递归,限制条件是k个数其和为n
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>> ans;
vector<int> cur;
dfs(k,n,,cur,ans);
return ans;
}
void dfs(int k,int n, int level, vector<int> &cur, vector<vector<int>> &ans)
{
if(k==&&n==)
{
ans.push_back(cur);
return;
}
if(k==)return;
for(int i=level;i<=;i++)
{
cur.push_back(i);
dfs(k-,n-i,i+,cur,ans);
cur.pop_back();
}
}
IV 简单dp,dfs超时,记忆化dfs应该可以
dp[]=;
for(int i=;i<=target;i++)
{
for(int j=;j<nums.size();j++)
{
if(nums[j]<=i)dp[i]+=dp[i-nums[j]];
}
}
return dp[target];
combination sum(I, II, III, IV)的更多相关文章
- Leetcode 39 40 216 Combination Sum I II III
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- LeetCode: Combination Sum I && II && III
Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...
- Two Sum I & II & III & IV
Two Sum I Given an array of integers, find two numbers such that they add up to a specific target nu ...
- 买卖股票的最佳时机I II III IV
I 假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润. II 假设有一个数组,它的第i个元素是一个给定的股票 ...
- LeetCode:Combination Sum I II
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- 子集系列(二) 满足特定要求的子集,例 [LeetCode] Combination, Combination Sum I, II
引言 既上一篇 子集系列(一) 后,这里我们接着讨论带有附加条件的子集求解方法. 这类题目也是求子集,只不过不是返回所有的自己,而往往是要求返回满足一定要求的子集. 解这种类型的题目,其思路可以在上一 ...
- 1. Two Sum I & II & III
1. Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- Path Sum I && II & III
Path Sum I Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that ad ...
- Two Sum(II和IV)
本文包含leetcode上的Two Sum(Python实现).Two Sum II - Input array is sorted(Python实现).Two Sum IV - Input is a ...
随机推荐
- Flask组件
组件踩坑记录 : 先注册组件在使用配置(...) flask-script Flask Script扩展提供向Flask插入外部脚本的功能,包括运行一个开发用的服务器,一个定制的Python shel ...
- jQuery.extend()参数
非原创,转载仅供学习 在处理插件参数的接收上,通常使用jQuery的extend方法.extend方法传递单个对象的情况下,这个对象会合并到jQuery身上,而当用extend方法传递一个以上的参数时 ...
- MYSQL 查看最大连接数和修改最大连接数
MySQL查看最大连接数和修改最大连接数 1.查看最大连接数show variables like '%max_connections%';2.修改最大连接数set GLOBAL max_connec ...
- SQL Server统计数据库中表个数、视图个数、存储过程个数
表个数 SELECT count(*) FROM sys.objects WHERE type='U' 视图个数 SELECT count(*) FROM sys.objects WHERE type ...
- T-SQL DISTINCT子句 去重复
语法 以下是DISTINCT关键字的基本语法,用于删除重复记录. SELECT DISTINCT column1, column2,.....columnN FROM table_name WHERE ...
- ASP.NET Core之NLog使用
1.新建ASP.NET Core项目 1.1选择项目 1.2选择.Net版本 2. 添加NLog插件 2.1 通过Nuget安装 2.2下载相关的插件 3.修改NLog配置文件 3.1添加NLog配置 ...
- [C]C语言中的指针和内存泄漏几种情况
引言 原文地址:http://www.cnblogs.com/archimedes/p/c-point-memory-leak.html,转载请注明源地址. 对于任何使用C语言的人,如果问他们C语言的 ...
- elasticsearch中的java.io.IOException: 远程主机强迫关闭了一个现有的连接
[2018-07-31T14:29:41,289][WARN ][o.e.x.s.t.n.SecurityNetty4HttpServerTransport] [9rTGh-y] caught exc ...
- grep,find
grep是强大的文本搜索工具,他可以对文件逐行查看,如果找到匹配的模式,就可以打印出包含次模式的所有行,并且支持正则表达式 find查找文件的grep是来查找字符串的,文件的内容 grep 文件的内容 ...
- STM32L476应用开发之八:便携式气体分析仪项目总结
在本次项目中,我们实现的实际上是2套设备:便携式氧气分析仪以及便携式甲烷分析仪.但这两台仪器实际使用的主控板我们是设计了一套,所以主控板是适合于这两个设备的. 1.硬件设计 便携式气体分析仪的功能比较 ...