【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 ...
随机推荐
- EC2 Instance扩容EBS卷容量
EC2实例运行一段时间后,由于日志和一些应用程序数据的积累,可能出现之前预留的磁盘容量不够需要扩容的情况.AWS EBS目前还不支持在线扩容,不过可以通过结合snapshot来实现. 如,我的EC2 ...
- IDEA里如何安装Python插件打造开发环境(图文详解)
前言 python是一种功能强大和适用面很广的开发语言,在大数据应用和机器学习日益流行的年代,python凭借其简洁.易用和可扩展性获得很多用户的支持,近年来使用率高速增长.python环境下,集成了 ...
- 取消文件与svn服务器的关联
在使用svn项目管理工具的时候,经常遇到这样的情况: 我从svn下载下来了一个版本,后面不在需要和svn进行同步版本管理,但是文件夹的上面总是有一个勾,显示同步状态,强迫症真的受不了. 效果见小图: ...
- 2、编写/修改权限及执行Shell程序的步骤
学习目标编写Shell程序执行Shell程序 正文Shell程序有很多类似C语言和其他程序设计语言的特征,但是又没有程序语言那样复杂.Shell程序是指放在一个文件中的一系列Linux命令和实用程序. ...
- Android如何避免输入法弹出时遮挡住按钮或输入框
在AndroidManifest.xml中为对应的activity添加android:windowSoftInputMode="adjustResize" 在AndroidMani ...
- Struts2页面配置和访问servlet API
一.Struts2页面配置 在struts2中页面可以分为两种,全局页面和局部页面. 1.全局页面: 在一个<package></package>标签内的多个action都要跳 ...
- 二叉树数组C++实现
基本概念梳理 孩子:子结点 双亲:父节点 度:有多少个子结点 有序树:固定的排列的树 无序树:排列与顺序无关的树 二叉树:所有结点小于等于2的树 源代码:https://github.com/cjy5 ...
- 44个 Javascript 变态题解析
原题来自: http://javascript-puzzlers.herokuapp.com/ 读者可以先去做一下感受感受. 当初笔者的成绩是 21/44... 当初笔者做这套题的时候不仅怀疑智商, ...
- PS基础,英语
PS基础(矢量图案的绘制) 水平参考线:新建背景(长宽一致,背景内容为透明)---设置水平参考线(水平垂直都要)---完成. 背景制作:设置前景色---用矩形选框工具绘制正方形选区(背景已被参考线平分 ...
- 使用axis2调用webservice需要导入的依赖
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> &l ...