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. spring-cloud-eureka服务注册与发现

    Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的.SpringCloud将它集成在其子项 ...

  2. JMeter 测试中,使用 JDBC 查询条件为中文时解决办法

    在 JMeter 的 jdbc 链接配置插件上,连接 mysql 的 url上加useUnicode=true&characterEncoding=UTF-8即可解决问题.

  3. 文本超出显示省略号/数字英文字母折行有关css 属性/显示两行,第二行省略号显示css方法

    文本超出显示省略号: 定容器宽度 overflow: hidden; text-overflow: ellipsis; 文本超出显示省略号 white-space:nowrap;  强制文本不换行 折 ...

  4. Linux服务器常用工具

    一.Ubuntu18版本 服务器 1)替换源.将原有的源注释掉 vi /etc/apt/sources.list deb http://mirrors.aliyun.com/ubuntu/ bioni ...

  5. [转] webpack中配置Babel

    一.安装 npm install --save-dev babel-loader babel-core babel-preset-env 二.在webpack.config.js中配置module 1 ...

  6. 【JAVA】高并发优化细节点

    高并发优化细节点: 微服务化 如何发现系统瓶颈?   如何高效利用有限内存: 使用基本类型 使用数组,不用集合 自定义map与数据结构   Integer—>int, Set<Intege ...

  7. 记一次线上Zabbix对Redis监控实录

    前言:Redis作为缓存服务器我想大家都比较的熟悉,那么,如果想要更好的维护和监控,那么我们会对其redis服务器统一监控起来,如何监控呢?如果在生产环境一台服务器部署多个redis,这样就会出现多个 ...

  8. Ansible入门篇:playbook的使用

    playbooks介绍 playbooks是 一个不同于使用Ansible命令行执行方式的模式,其功能更强大灵活.简单来说,playbook是一个非常简单的配置管理和多主机部署系统,不同于任何已经存在 ...

  9. vuex脑图

  10. 20175305张天钰《java程序设计》第七周学习总结

    <java程序设计>第七周学习总结 第八章 常用实用类 1.String类 1.String类不可以有子类. 2.用户无法输出String对象的引用,输出的是字符序列 3.构造方法:Str ...