【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 ...
随机推荐
- SpringCloud---分布式配置中心---Spring Cloud Config
1.概述 1.1 Spring Cloud Config是Spring Cloud的一个全新项目: 作用:为分布式系统中的基础设施.微服务应用提供集中化的外部配置支持: 分为服务端.客户端2个 ...
- 浅解bootstrap 下拉框插件
最近的业务需求是把后台数据渲染到前端,其中有一个是复选框数组,前端选择渲染的方式是bootstrap的下拉插件 渲染完成之后,此时会在原有select同级上面生成buton,dropdown-menu ...
- WIndows下 Pycharm 配置Anaconda和TensorFlow
1安装PyCharm 前提安装Python,然后去官网下载PyCharm 2 安装Anaconda 在官网下载Anaconda,并安装 下载好之后就跟安装一般的软件没啥区别,选择自己安装的文件夹,下一 ...
- 透视效果shader(边缘光)
思路:渲染两次. 1.第一次渲染:利用Greater进行深度测试,当目标被遮挡时,用一个边缘光的效果显示. 2.第二次渲染:正常渲染. 边缘光的思路:观察方向和顶点法向量夹角越大,边缘光越明显.边缘光 ...
- Binder 驱动(三)
Binder 驱动是 Binder 的最终实现, ServiceManager 和 Client/Service 进程间通信最终都是由 Binder 驱动投递的. Binder 驱动的代码位于 ker ...
- Git创建版本库和添加文件
创建版本库: 首先新建一个目录,然后通过 $ git init命令把这个目录变成Git可管理的仓库,例如: 在"C:\AmyPersonal\AmyGit"路径下新建文件夹Git1 ...
- 深入java虚拟机学习 -- 类的卸载
类的生命周期 在开始本节之前让我们再来回顾下类的生命周期 没看过前6个过程的同学建议从头看下<深入java虚拟机学习 -- 类的加载机制>,这里就不再过多介绍了,着重说下类的卸载 类的卸载 ...
- 开启Unity项目中VS工程的属性面板
https://www.cnblogs.com/CodeGize/p/7859656.html Unity生成的VS工程一般是无法打开属性面板的.但是事实上,如果使用VS for unity,通过配置 ...
- wcf 基本配置
<system.serviceModel> <services> <service name="ServiceUpdater.ServiceUpdate&quo ...
- 在 Azure 上创建和链接 MySQL 数据库
本快速入门介绍了如何使用 Azure 门户创建并连接 MySQL 数据库.在本教程中完成的所有操作均符合 1 元试用条件. 开始之前如果您还没有 Azure 账户,可以申请 1 元试用账户 步骤1:创 ...