【一天一道LeetCode】#39. Combination Sum
一天一道LeetCode系列
(一)题目
Given a set of candidate numbers (C) 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.
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3]
(二)解题
/*
主要思路:
1.对目标vector排序,定义一个临时的vector容器tmp
2.采用动态规划和回溯法的方法,对于当前要添加到tmp的数num
(1)如果target<num,return;
(2)如果target>num,则继续在num和num以后的数中选择一个数相加;(此处需要考虑可以重复的组合)
(3)如果target==num,则代表找到
对于(2),(3)递归完成后需要将压入的数弹出,即采用回溯法的思想
*/
class Solution {
public:
vector<vector<int>> ret;//结果
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(),candidates.end());//首先进行排序
for(int idx = 0;idx<candidates.size();idx++)
{
if(idx-1>=0 && candidates[idx] == candidates[idx-1]) continue;//避免重复查找
else{
if(candidates[idx]<=target){//如果小于则调用动态规划函数
vector<int> tmp;
tmp.push_back(candidates[idx]);
combinationDfs(candidates,tmp,idx,target-candidates[idx]);
}
}
}
return ret;
}
/*candidates为目标vector
tmp为临时vector变量,存储找到的组合
start是初始序号,代表在start及其以后的数字中查找
target是当前需要在candidates中查找的数
*/
void combinationDfs(vector<int>& candidates , vector<int>& tmp , int start,int target)
{
if(target == 0){//如果target等于0,代表第一个数就满足
ret.push_back(tmp);
return;
}
for(int idx = start;idx<candidates.size();idx++)
{
if(target > candidates[idx])
{
tmp.push_back(candidates[idx]);
combinationDfs(candidates,tmp,idx,target-candidates[idx]);
tmp.pop_back();//回溯法,要pop出最后压入的元素
}
else if(target == candidates[idx])//等于target代表以及找到
{
tmp.push_back(candidates[idx]);
ret.push_back(tmp);
tmp.pop_back();//回溯法,要pop出最后压入的元素
}
else if(target < candidates[idx])
{
return;
}
}
}
};
【一天一道LeetCode】#39. Combination Sum的更多相关文章
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- [LeetCode] 39. Combination Sum 组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- LeetCode 39. Combination Sum (组合的和)
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...
- Java [Leetcode 39]Combination Sum
题目描述: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode 39 Combination Sum(满足求和等于target的所有组合)
题目链接: https://leetcode.com/problems/combination-sum/?tab=Description Problem: 给定数组并且给定一个target,求出所 ...
- [LeetCode] 39. Combination Sum ☆☆☆(数组相加等于指定的数)
https://leetcode.wang/leetCode-39-Combination-Sum.html 描述 Given a set of candidate numbers (candidat ...
- Leetcode 39. Combination Sum
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...
- leetcode 39 Combination Sum --- java
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- [leetcode]39. Combination Sum组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
随机推荐
- Mac入门——快捷键
快捷键: 截图 Shift+Command+3 截取全屏幕至桌面 Shift+Command+4 截取部分屏幕至桌面 Shift+Command+4+空格 截取窗口或原件至桌面 Shift+ ...
- boost::asio::spawn 将一统C++网络库
boost::asio::spawn 将一统C++网络库(金庆的专栏)boost::asio::spawn()创建一个协程,使C++网络编程大大简化,个人认为这使得 asio 成为C++首选网络库.b ...
- EBS技术开发之返利开发
返利是指公司间应收款项按一定比率的返还给客户.返利开发实质就是实现对应收发票的更改和新增(暂时我的理解) 一.对发票行更改 PACKAGE AP_INVOICE_LINES_ALL_PRIVATE I ...
- mysql 远程连接配置
近期买了阿里云服务器,服务器 安装了mysql,需要远程操作mysql数据库,但是远程不配置的话,连接不上去的.需要配置 .具体的配置如下: 先看看my.cnf是否绑定了本机,如果绑定了地址就解绑吧. ...
- SQLite 创建表(http://www.w3cschool.cc/sqlite/sqlite-create-table.html)
SQLite 创建表 SQLite 的 CREATE TABLE 语句用于在任何给定的数据库创建一个新表.创建基本表,涉及到命名表.定义列及每一列的数据类型. 语法 CREATE TABLE 语句的基 ...
- Java 中的日期与时间
Java 日期时间 标签 : Java基础 Date java.util.Date对象表示一个精确到毫秒的瞬间; 但由于Date从JDK1.0起就开始存在了,历史悠久,而且功能强大(既包含日期,也包含 ...
- jQuery Ajax 使用 ($.ajax、$.post、$.get)
项目中只要涉及到前后台的交互,数据状态之间的交互,ajax是必不可少的.一般项目中jquery方式的ajax用的还是比较多的.封装的比较好,用起来也顺手,兼容浏览器之间的差异. 操作的方式有三种: 1 ...
- 1gitolite构建git服务器
软件环境:在有网络条件下(主要是为了安装软件),UbuntuKylin 14.04 1 安装openssh-serveropenssh-client,如果用的是VPS之类的一般都默认安装好了,不 ...
- 软考下午题详解---uml图
在上篇博客中,小编主要简单的对软考下午题当中的数据流图设计进行了一系列总结,今天我们继续来看软考下午题当中大题部分,uml图的相关知识,在我们学习的过程中,我们也已经接触过,西安交大刘惠老师讲解过um ...
- 使用TT模板+mvc+wcf实现简单查询
今天是除夕,小编的这篇博客是掐着点儿发的,在此,祝各位小伙伴新年快乐,身体健康,万事如意:喜从天降,欣喜若狂:喜气盈门,好事成双:好人好运,金玉满堂:神采飞扬,如愿以偿,财源滚滚来,福如东海长:伴随着 ...