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)的更多相关文章

  1. 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 ...

  2. LeetCode: Combination Sum I && II && III

    Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...

  3. 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 ...

  4. 买卖股票的最佳时机I II III IV

    I 假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润. II 假设有一个数组,它的第i个元素是一个给定的股票 ...

  5. LeetCode:Combination Sum I II

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  6. 子集系列(二) 满足特定要求的子集,例 [LeetCode] Combination, Combination Sum I, II

    引言 既上一篇 子集系列(一) 后,这里我们接着讨论带有附加条件的子集求解方法. 这类题目也是求子集,只不过不是返回所有的自己,而往往是要求返回满足一定要求的子集. 解这种类型的题目,其思路可以在上一 ...

  7. 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 ...

  8. 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 ...

  9. Two Sum(II和IV)

    本文包含leetcode上的Two Sum(Python实现).Two Sum II - Input array is sorted(Python实现).Two Sum IV - Input is a ...

随机推荐

  1. java 解压缩 中文名称问题

    public List<String> unZip(String pathString, String zipPathString) { long startTime = System.c ...

  2. python结合pyvmomi 监控esxi的磁盘等信息

    1.安装python3.6.6 # 安装依赖,一定要安装,否则后面可能无法安装一些python插件 yum -y install zlib-devel bzip2-devel openssl-deve ...

  3. 用于主题检测的临时日志(c5ac07a5-5dab-45d9-8dc2-a3b27be6e507 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

    这是一个未删除的临时日志.请手动删除它.(5051e554-d10d-4e48-b2ca-37c38a30153a - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

  4. laravel sql复杂语句,原生写法----连表分组

    ### 使用了临时表.又分组又连表的感觉好难写,使用拉 ravel 但是现在越来也相信,没解决一个新的难题,自己又进步了一点点 ### 原生的sql: select user_code, realna ...

  5. 39)django-XSS 过滤

    使用kingedit别人是可以输入script代码.这在后台是不允许script代码运行的. 这里主要使用beatifulSoup过滤 示例1 beatufulsoup4 from bs4 impor ...

  6. select下拉框插件jquery.editable-select

    项目中有个需求,下拉框既可以下拉选择,也可以手动填写 html代码 <span>数据来源</span> </select> js代码 $('#noMean').ed ...

  7. C# WINFORM 编程中,选择**文件夹**而不是文件的方法(转)

    我们选择文件可以用 OpenFileDialog ,但是文件夹有两种方法. 法一: 用C#的FolderNameEditor类的子类FolderBrowser类来实现获取浏览文件夹对话框的功能.下面来 ...

  8. 《Oracle DBA工作笔记:运维、数据迁移与性能调优》 PDF 下载

    一:下载途径 二:本书图样 三:本书目录 第1篇 数据库运维篇第1章 数据库安装配置1.1 安装前的准备 11.2 安装数据库软件 51.2.1 方法1:OUI安装 61.2.2 方法2:静默安装 8 ...

  9. Android UiAutomator - CTS Frame

    使用UiAutomator进行UI自动化测试后,生成的测试结果并不是很美观.为了生成一份好看的测试结果(报告),本文将使用CTS框架,当然也可以自己编写一份测试报告框架(如:生成html,excel报 ...

  10. 如何在cmd中执行python文件

    打开cmd终端 输入python     然后再输入要执行文件的路径 就可以把python文件运行起来                                                  ...