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. requests库入门09-OAUTH认证

    实际登陆中,认证用到的token会变的,不过可以在GIthub设置一个私人token. 如图,登录GIthub,然后用户下面选择Settings/Developer settings/Personal ...

  2. 【转】C++拷贝构造函数详解

    一.什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是很简单的,例如: ; int b=a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量. 下面看一个类对象拷贝 ...

  3. less/sass 基础base文件

    less less-base.less文件展示: //清除标签默认样式; .label(){ ;;;_background-image:url(n1othing.txt)} ;;; font-size ...

  4. vuex之 mapState, mapGetters, mapActions, mapMutations 的使用

    一.介绍 vuex里面的四大金刚:State, Mutations,Actions,Getters (上次记得关于vuex笔记 http://www.cnblogs.com/adouwt/p/8283 ...

  5. [Linux]PHP-FPM与NGINX的两种通讯方式

    一.通过监听TCP端口通讯 php-fpm.d/www.conf ; The address on which to accept FastCGI requests. ; Valid syntaxes ...

  6. Android下利用zbar类库实现扫一扫

    程序源代码及可执行文件下载地址:http://files.cnblogs.com/rainboy2010/zbardemo.zip Android下常用的条码扫描类库有zxing和zbar,比较了一下 ...

  7. 监听本机tcp和udp的端口

    #!/bin/bash #tcp part port1=`netstat -an|grep LISTEN|egrep "0.0.0.0|:::"|awk '/^tcp/ {prin ...

  8. mybatis 按in 函数参数顺序排序

    使用 FIELD()函数 SELECT *  FROM   user  WHERE id IN (72, 80, 69)  ORDER BY FIELD(id, 72, 80, 69)

  9. swift 学习- 24 -- 协议 01

    // 协议 定义了一个蓝图, 规定了用来实现某一特定任务或者功能的方法, 属性, 以及其他需要的东西. // 类, 结构体, 或 枚举都可以遵循协议, 并且为协议定义的这些要求 提供具体的实现, 某个 ...

  10. 关于 IOS 时间的一下用法

    1. 加减月份 -(NSDate *)getPriousorLaterDateFromDate:(NSDate *)date withMonth:(int)month {     NSDateComp ...