leetcode111:combination-sum
题目描述
- 题目中所有的数字(包括目标数T)都是正整数
- 你给出的组合中的数字 (a 1, a 2, … , a k) 要按非递增排序 (ie, a 1 ≤ a 2 ≤ … ≤ a k).
- 结解集中不能包含重复的组合
[2, 2, 3]
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 (a 1, a 2, … , a k) must be in non-descending order. (ie, a 1 ≤ a 2 ≤ … ≤ a k).
- The solution set must not contain duplicate combinations.
For example, given candidate set2,3,6,7and target7,
A solution set is:
[7]
[2, 2, 3]
class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
vector <vector<int>>res;
vector<int> temp;
sort(candidates.begin(),candidates.end());
Sum(res,temp,candidates,0,target);
return res;
}
void Sum(vector <vector <int>> &res,vector<int> &temp,vector<int >&candidates,int k,int target){
if (target==0){
res.push_back(temp);
return ;
}
if (target <0 || k>=candidates.size())
return ;
vector<int> t =temp;
temp.push_back(candidates[k]);
Sum(res,temp,candidates,k,target-candidates[k]);
Sum(res,t,candidates,k+1,target);
}
};
leetcode111:combination-sum的更多相关文章
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LeetCode:Combination Sum I II
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- Combination Sum | & || & ||| & IV
Combination Sum | Given a set of candidate numbers (C) and a target number (T), find all unique comb ...
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 【leetcode】Combination Sum
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- LeetCode Combination Sum III
原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...
随机推荐
- P2034 选择数字 / P2627 [USACO11OPEN]Mowing the Lawn G
Link 题目描述 给定一行 \(n\) 个非负整数 \(a[1]..a[n]\) .现在你可以选择其中若干个数,但不能有超过 \(k\) 个连续的数字被选择.你的任务是使得选出的数字的和最大. 输入 ...
- linux下各种骚操作
(备注:不定时更新) 1. ctrl+l 清屏快捷键,相当于clear 2. !+命令开头部分 执行最近执行的此条命令 ### 如!vi 编辑上一次用vi打开的文件, 3. echo $$ ...
- javaOOP9.17刷题
今天在吃晚饭和朋友们聊天的时候,一个已经在自己写java全栈项目并且准备面试的同学说要我好好学习OOP这部分,他现在写代码写面试题,发现自己都忘了基类派生类调用构造函数时候的顺序是什么样的巴拉巴拉,说 ...
- thinkphp6.0.x 反序列化详记(一)
前言 这几天算是进阶到框架类漏洞的学习了,首当其冲想到是thinkphp,先拿thinkphp6.0.x来学习一下,体验一下寻找pop链的快乐. 在此感谢楷师傅的帮忙~ 环境配置 用composer指 ...
- Linux系统常用API总结
1.错误处理 - fprintf() - perror() 2.通用I/O模型 - fd = open(pathname, flags, mode) - numread = read(fd, buff ...
- 对NETIF_F_GSO的一些理解
看linux内核协议栈的时候看到tcp_sendmsg函数,看起来并不难理解,但是申请skb的时候主buff大小让我很困惑.我以前一直以为会根据sack/ip option/pmtu等计算一个mss, ...
- 多测师讲解selenium_alert弹框定位_高级讲师肖sir
from selenium import webdriverfrom time import sleepdrvier=webdriver.Chrome()url=r'F:\dcs\DCS课程安排\se ...
- day32 Pyhton 异常处理
一.内容回顾 反射的另外两个内置函数 setattr delattr a.b=c 与 setattr(a,'b',c)相对 del a.b 与 delattr(a,'b') 两个内置函数 A,B(A) ...
- Mysql架构与内部模块-第三章
前言 接上文,本篇文章专门简述Mysql存储引擎,内容繁多,如果你只需知道每种存储引擎的适用场景,可以直接查看本文最后列出的适用场景部分. 正文: Mysql存储引擎作为本系列文章中相对重要的一环,也 ...
- 【数论】HDU 4143 A Simple Problem
题目内容 给出一个正整数\(n\),找到最小的正整数\(x\),使之能找到一个整数\(y\),满足\(y^2=n+x^2\). 输入格式 第一行是数据组数\(T\),每组数据有一个整数\(n\). 输 ...