LeetCode题解39.Combination Sum
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\)
则递归形式:
\]
这个递推公式包含了重复元素利用的情况(\(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的更多相关文章
- [Leetcode][Python]39: Combination Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...
- 【LeetCode】39. Combination Sum (2 solutions)
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- leetcode个人题解——#39 Combination Sum
思路:先对数据进行排序(看评论给的测试数据好像都是有序数组了,但题目里没有给出这个条件),然后回溯加剪枝即可. class Solution { public: ; vector<vector& ...
- 【一天一道LeetCode】#39. Combination Sum
一天一道LeetCode系列 (一)题目 Given a set of candidate numbers (C) and a target number (T), find all unique c ...
- LeetCode OJ 39. Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- LeetCode:39. Combination Sum(Medium)
1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...
- 【LeetCode】39. Combination Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:[htt ...
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
随机推荐
- selenium截图
文件结构 1.DateUtil.py # cncoding = utf-8 import time from datetime import datetime ''' 本文件主要用于获取当前的日期以及 ...
- poj1456 Supermarket 贪心+并查集
题目链接:http://poj.org/problem?id=1456 题意:有n个物品(0 <= n <= 10000) ,每个物品有一个价格pi和一个保质期di (1 <= pi ...
- 给JS包写TypeScript用的类型申明文件
TS (TypeScript)区别于JS (JavaScript)一个最大的不同是TS增加了类型.当一些TS代码要使用JS包的时候,最好这些JS包都有类型介绍,比如这个变量是什么类型,那个函数参数的什 ...
- redis深入了解
来自:https://www.cnblogs.com/lixinjie/p/a-key-point-of-redis-in-interview.html 是数据结构而非类型 很多文章都会说,redis ...
- 日常报错记录2: MyBatis:DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.------------ Cause: java.lang.NoSuchMethodException: com.offcn.dao.ShopDao.<init>()
直接上干货: 报错归纳1: DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4 ...
- Linux-网络基础
1.如何将俩台不同网络中的电脑通信,见下图
- 如何在电脑上配置两个tomcat
问题 准备逐渐转向idea的怀抱了,每次部署项目时和eclipse使用的都是同一个tomcat,这是很大的隐患,并且非常的不方便,遂再配置一个tomcat 1.下载tomcat和配置系统变量 CATA ...
- 分分钟解决MySQL查询速度慢与性能差
阅读本文大概需要 6 分钟. 一.什么影响了数据库查询速度 1.1 影响数据库查询速度的四个因素 1.2 风险分析 QPS: QueriesPerSecond意思是“每秒查询率”,是一台服务器每秒能够 ...
- Win10家庭版WindowsUpdate属性为灰色
一般的取消Windows更新只需要打开任务管理器,点击服务 然后点击左下角的打开服务 找到WindowsUpdate,右键属性 按照正常的电脑只要在启动类型中选择禁用,然后在恢复里的第一次操作选择无操 ...
- Python----爬虫入门系列等
欢迎访问我的人生苦短系列(目前主要是Python爬虫入门) 传送门:https://www.jeson.xin/category/%E4%BA%BA%E7%94%9F%E8%8B%A6%E7%9F%A ...