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 ...
随机推荐
- 《剑指offer》二叉树的深度
本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结:
- [转载 java 技术栈] eclipse 阅读跟踪 Java 源码的几个小技巧!
本文基于Eclipse IDE,我们每天都使用的IDE其实提供了很多强大的功能,掌握它们,往往能够事半功倍. 1.Quick Type Hierarchy 快速查看类继承体系. 快捷键:Ctrl + ...
- zabbix 修改为UTC 时区的配置
修改php.ini中的date.timezone = UTC还确实是正解,修改后要重新启动apache,另外你应该用phpinfo()检查一下你修改php.ini和phpinfo()中指明的当前php ...
- 解析xml,返回第一级元素键值对。如果第一级元素有子节点,则此节点的值是子节点的xml数据。
/** 转换成XML格式字符串 **/ public static String doXMLStr(Map<String, String> map) { StringBuffer xml_ ...
- 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165323
1.Kali下载路径 由于之前的课程中已经安装过VMware虚拟机,这一个步骤就省去 2.安装 (1)创建新的虚拟机->典型->添加镜像路径 (2)选择操作系统linux (3)进行虚拟机 ...
- python第十六天
一.包 1.什么是包? 一系列模块的集合 2.有什么用? 包通过文件夹来管理一系列功能相近的模块 3.包重点? 包中一定有一个专门用来管理包中所有模块的文件 __init__ 4.什么是包名? 包 ...
- PhpStorm代码提示(省电模式)的设置与使用
PhpStorm中有个,Power Save Mode(省电模式),开启则代码不能自动提示,关闭则可以. 开启/关闭: 1.点击“File”菜单,最下面,Power Save Mode,勾选或取消: ...
- DICOM图像转出为bmp格式图像方法(matlab程序实现)
在matlab中用dicomread读取dicom文件后,生成一个MxN矩阵(对应图像像素个数),每个像素灰度数据是int16格式 但是bmp图像灰度是int8格式的(灰度范围0~255),所以若想把 ...
- [开源] C# 封装 银海医保的接口
Github 地址: https://github.com/zifeiniu/YinHaiYiBaoCSharpAPI C#Model封装 银海医保的接口 介绍 银海医保的接口我就不说了,很多家医院在 ...
- sqlzoo:6
第一個例子列出球員姓氏為'Bender'的入球數據. * 表示列出表格的全部欄位,簡化了寫matchid, teamid, player, gtime語句. 修改此SQL以列出 賽事編號matchid ...