【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 ...
随机推荐
- python3.5+ asyncio await异步详解
import asyncio,time from collections import defaultdict from pprint import pprint collect=defaultdic ...
- golang mcall
// func mcall(fn func(*g)) // Switch to m->g0's stack, call fn(g). // Fn must never return. It sh ...
- JS的正则表达式 - RegExp
RegExp 对象 RegExp 对象表示正则表达式,它是对字符串执行模式匹配的强大工具. 正则表达式的创建方式 1.文字格式,使用方法如下: /pattern/flags (即:/模式/标记) 2 ...
- Java的IO流各个类的使用原则
参考:http://blog.csdn.net/ilibaba/article/details/3955799 Java IO 的一般使用原则(花多眼乱,其实每个类都有专门的作用): 这里有详细介绍: ...
- Django 入门项目案例开发(下)——创建项目应用及模型类
关注微信公众号:FocusBI 查看更多文章:加QQ群:808774277 获取学习资料和一起探讨问题. 前面两章是在已经开发好的项目上用来描述环境和业务,这一章创建一个全新的项目来用作开发,你可以跟 ...
- 腾讯云AI应用产品总监王磊:AI 在传统产业的最佳实践
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 背景:5月23-24日,以"焕启"为主题的腾讯"云+未来"峰会在广州召开,广东省各级政府机构领导.海 ...
- js跳转指定的网站
$(function () {window.location.replace("http:new.mingyikanya.com");});
- Lucene学习之四:Lucene的索引文件格式(3)
本文转载自:http://www.cnblogs.com/forfuture1978/archive/2010/02/02/1661436.html ,略有删改和备注. 四.具体格式 4.2. 反向信 ...
- Azure 项目构建 – 部署高可用的 Python Web 应用
Python 以其优美,清晰,简单的特性在全世界广泛流行,成为最主流的编程语言之一.Azure 平台针对 Python 提供了非常完备的支持.本项目中,您将了解如何构造和部署基于 Azure Web ...
- IE11 F12 开发人员工具 查看 Cookie
参考网址:Using the F12 developer tools in IE11 Step1 : IE11 => F12 打开 开发人员工具 Step2:开发人员工具 => 网络F5 ...