【Leetcode】【Medium】Combination Sum
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]
解题思路:
使用回溯的思想穷举可能的结果。
代码:
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> lst;
vector<int> ans;
sort(candidates.begin(), candidates.end());
Backtracking(lst, ans, candidates, , target);
return lst;
} void Backtracking(vector<vector<int>> &lst, vector<int> ans, vector<int> candidates, int idx, int left) {
for (int i = idx; i < candidates.size(); ++i) {
int cur_left = left - candidates[i];
if (cur_left == ) {
ans.push_back(candidates[i]);
lst.push_back(ans);
return;
}
if (cur_left > ) {
ans.push_back(candidates[i]);
Backtracking(lst, ans, candidates, i, cur_left);
ans.pop_back();
} else {
return;
}
}
}
};
另:是否还有DP/DFS等其他思路。
【Leetcode】【Medium】Combination Sum的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [Leetcode 40]组合数和II Combination Sum II
[题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...
- [Leetcode 39]组合数的和Combination Sum
[题目] Given a set of candidate numbers (candidates) (without duplicates) and a target number (target) ...
- 【leetcode刷题笔记】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode题意分析&解答】39. Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 【LeetCode每天一题】Combination Sum II(组合和II)
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- 【leetcode刷题笔记】Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
随机推荐
- IBM Worklight OutOfMemoryError: Java heap space 错误
在启动 IBM Worklight 6.0 server 的时候,报了一个 OutOfMemory 的错误: [INFO ] FWLSE4006I: Worklight Studio is ...
- Python 如何计算当前时间减少或增加一个月
问题 今天在之前的代码中发现了一个bug,有个计算当前时间减少一个月的函数,其报出下面的异常信息: ValueError: day is out of range for month 看一下代码: i ...
- 错误:严重: Servlet.service() for servlet [appServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is
严重: Servlet.service() for servlet [appServlet] in context with path [] threw exception [Request proc ...
- .netCore2.0 过滤器
不同的过滤器类型会在执行管道的不同阶段运行,因此他们各自有一套自己的应用场景.可以根据不同的业务需求和在请求管道中的执行位置来选择合适创建的过滤器.运行与MVC Action调用管道内的过滤器有时候被 ...
- Centos时间查看修改命令date详解
1.查看.修改Linux时区与时间 一.linux时区的查看与修改 1,查看当前时区date -R 2,修改设置时区方法1:tzselect 方法2:仅限于RedHat Linux 和 CentOSt ...
- [转]NLog 自定义字段 写入 oracle
本文转自:http://www.cnblogs.com/skyapplezhao/p/5690695.html 1.通过Nuget安装NLog 下载,简单入门 请参照 我刚才转的几篇文章,下面我直接贴 ...
- Linux 上安装Docker 并部署netcor2.1
述 容器,顾名思义是用来存放并容纳东西的器皿: 而容器技术伴着Docker的兴起也渐渐的映入大家的眼帘,它是一个抽象的概念,同时也是默默存在世上多年的技术,不仅能使应用程序间完全的隔离,而且还能在共享 ...
- js扩展
http://www.css88.com/doc/underscore/#findWhere
- Lucene学习之二:Lucene的总体架构
本文转载自:http://www.cnblogs.com/forfuture1978/archive/2009/12/14/1623596.html Lucene总的来说是: 一个高效的,可扩展的,全 ...
- 9、springboot之处理静态资源
在springboot项目中的resource根目录下建立三个文件夹static.public.resources 里面都放同样名字的图片 但是图片内容不一样 启动springboot之后输入 htt ...