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. Nest + typeorm

    1\     Nest (https://nestjs.com/)  is a framework for building efficient, scalable Node.js web appli ...

  2. Tomcat 打开jmx

    jmx 配置后可以通过windows java客户端自带的jconsole.exe配置登陆,直观的查看jvm的情况及系统的各项指标: 一.配置linux下tomcat的jmx 具体配置如下,如果生产环 ...

  3. MVC 当前上下文中不存在名称“Styles” “Scripts”

    它们在程序集System.Web.Optimization下,只要全名引用即可 修改配置 在web项目的Views下的web.config修改如下即可,如果是Areas下,处理方法相同 <sys ...

  4. Vue 实战项目开发流程

    一 基础配备 ## 一.环境搭建 #### 1.安装node - 去[官网](https://nodejs.org/zh-cn/)下载node安装包 - 傻瓜式安装 - 万一安装后终端没有node环境 ...

  5. U盘被写保护不能重新格式化

    今天一个朋友拿给我一个U盘,说这个U盘是商家送的,他想格式化,但是U盘被写保护了,系统不能格式化. 他想把这个U盘插到车子里听音乐,但是车载系统始终识别的是第一个分区,而这个分区正是被写保护那个,且这 ...

  6. 大数据mapreduce二分法ip定位之Python实现

    ip定位数据大约12M,采用-chacheFile 分发 文件来源https://pan.baidu.com/s/1J0pwTafHgt4T0k3vV_gC-A 格式大致格式如下: 0.0.0.0 0 ...

  7. js---json对象拆分

    var a={ "bb":"world", "a0":1, "a1":2, "b0":4, &quo ...

  8. PID控制器开发笔记之三:抗积分饱和PID控制器的实现

    积分作用的引入是为了消除系统的静差,提高控制精度.但是如果一个系统总是存在统一个方向的偏差,就可能无限累加而进而饱和,极大影响系统性能.抗积分饱和就是用以解决这一问题的方法之一.这一节我们就来实现抗积 ...

  9. ionic3 git 提交报错

    npm ERR! cordova-plugin-camera@ gen-docs: `jsdoc2md --template "jsdoc2md/TEMPLATE.md" &quo ...

  10. LeetCode(120):三角形最小路径和

    Medium! 题目描述: 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] ...