39. Combination Sum

Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

For example, given candidate set [2, 3, 6, 7] and target 7,

My Thought

题目的大致意思:

给定一个非负整数的集合(不包含重复元素),以及给定一个目标数字 T,给出集合所有的子集,满足以下三个条件:

  • 该子集所有元素之和为目标数字 T
  • 每个子集允许元素重复
  • 不允许有相同的子集

给定的集合很规范:非负而且不包含重复元素。

看到数列先排序。这样按顺序遍历获得的解一定不重复。

想法:

从小到大排完列表后,递归求解。

我们要在对于 \(sorted\ list\) 范围 \([0,n-1]\)中求解子集满足题意。

记:

  • 目标整数记为 \(t\)
  • 求解过程为 \(find\)
  • 遍历数组C下标,记为 \(i\)

则递归形式:

\[find(i,t,n-1) = C[i] + find(i, t-C[i],n-1)
\]

这个递推公式包含了重复元素利用的情况(\(f(i,...)=C[i]+f(i,...)\))

伪代码:

sort(C);   // C范围:[0,n-1]
// vector v:用来暂存一个解
// begin:当前处理下标
PROCEDURE find(v,target,begin)
if target<C[index]
return
if binary_search(begin,n-1)!= FALSE
v.push(SN) //SN为二分搜索找到的元素
ret.push(v)
for i = beg to n-1 do
temp = v
temp.push(C[i])
find(temp, target-C[i],i)

Code(C++ 16ms)

class Solution {
public:
vector<vector<int>> ret;
vector<int> v;
// binary search
int bs(vector<int>&nums, int l,int h, int t){
if(l<=h){
int mid = (l+h)/2;
if(nums[mid]<t)
return bs(nums,mid+1,h,t);
else if(nums[mid]>t)
return bs(nums,l,mid-1,t);
return mid;
}
return -1;
}
bool find(vector<int> vv,int n,int beg){
if(n<v[beg])
return false;
int pos=bs(v,beg,v.size()-1,n);
vector<int > temp=vv;
if(pos!=-1){
temp.push_back(v[pos]);
ret.push_back(temp);
}
for(int i=beg;i<v.size();++i){
temp=vv;
temp.push_back(v[i]);
find(temp, n-v[i], i);
}
return false;
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(),candidates.end());
v = candidates;
vector<int> vv;
find(vv,target,0);
return ret;
}
};

LeetCode题解39.Combination Sum的更多相关文章

  1. [Leetcode][Python]39: Combination Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...

  2. 【LeetCode】39. Combination Sum (2 solutions)

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

  3. leetcode个人题解——#39 Combination Sum

    思路:先对数据进行排序(看评论给的测试数据好像都是有序数组了,但题目里没有给出这个条件),然后回溯加剪枝即可. class Solution { public: ; vector<vector& ...

  4. 【一天一道LeetCode】#39. Combination Sum

    一天一道LeetCode系列 (一)题目 Given a set of candidate numbers (C) and a target number (T), find all unique c ...

  5. LeetCode OJ 39. Combination Sum

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  6. LeetCode:39. Combination Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...

  7. 【LeetCode】39. Combination Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:[htt ...

  8. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

  9. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

随机推荐

  1. Moq 在.net Core 单元测试中的使用

    Moq,主要用来伪造接口的实现类,实现方法,属性 moq The most popular and friendly mocking framework for .NET What? Moq (pro ...

  2. Linux计划任务及压缩归档

    计划任务介绍 自动执行,备份数据. crontab    和   at : at:它是一个可以处理仅执行一次就结束的指令 crontab:它是会把你指定的工作或任务,比如:脚本等,按照你设定的周期一直 ...

  3. 理解go的闭包

    package main import ( "fmt" ) func test(a int) { a++ fmt.Println(a) } func test2() func() ...

  4. 腾讯工蜂Git关联Jenkins Hooks

    现在国内外Git平台非常多,最近维护的腾讯工蜂免费公网版本git.code.tencent.com,免注册(建议使用微信登录,舒服)即可使用私有仓库.对小型团队体验还不错,如果要关联Jenkins进行 ...

  5. JS 实现blob与base64互转

    /** * base64 to blob二进制 */ function dataURItoBlob(dataURI) { var mimeString = dataURI.split(',')[0]. ...

  6. 2018—2019-- 2网络对抗技术20165239Exp信息搜集 漏洞扫描

    一.实验内容 二.实验步骤 1.各种搜索技巧的应用 2.DNS IP注册信息的查询 3.基本的扫描技术 主机发现 端口扫描 OS及服务版本探测 具体服务的查点 4.漏洞扫描 三.实验中遇到的问题 四. ...

  7. 关于<软件>的定义

    百度百科: 软件是一系列按照特定顺序组织的计算机数据和指令的集合.一般来讲软件被划分为系统软件.应用软件和介于这两者之间的中间件. 国标中的定义: 与计算机系统操作有关的计算机程序.规程.规则,以及可 ...

  8. leetcode刷题正则表达式

    题目链接:https://leetcode-cn.com/problems/regular-expression-matching/ 这道题用到了动态规划: 关于动态规划请参考这篇博文:https:/ ...

  9. 用canal同步binlog到kafka,spark streaming消费kafka topic乱码问题

    canal 1.1.1版本之后, 默认支持将canal server接收到的binlog数据直接投递到MQ, 目前默认支持的MQ系统有kafka和RocketMQ. 在投递的时候我们使用的是非压平的消 ...

  10. CTSC2017总结

    这个博客已经弃坑近一年了,自从去年国赛大力卡线进队后这近一年来我的情况从博客一年没更就可见一斑,OI水平原(zhi)地(xian)踏(fu)步(chong),炉石和双升的姿势水平倒是提高不少. 在经历 ...