Leetcode题解(4):L216/Combination Sum III
L216: Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.
Ensure that numbers within the set are sorted in ascending order.
Example 1:
Input: k = 3, n = 7
Output:
[[1,2,4]]
Example 2:
Input: k = 3, n = 9
Output:
[[1,2,6], [1,3,5], [2,3,4]]
解题思路:递归,注意一些条件的处理,避免不必要的处理
class Solution {
public:
bool combine(int min, int k, int n, vector<int> vec, vector<vector<int>>& result){
if(n<min*k) //更确切的。能够用等差数列公式算出最大值最小值
return false;
if(n>9*k)
return true;
if(k==1)
{
vec.push_back(n);
result.push_back(vec);
return true;
}
for(int i=min;i<=9;i++)
{
vec.push_back(i);
bool rt = combine(i+1, k-1,n-i, vec, result);
vec.pop_back();
if(!rt)
break;
}
return true;
}
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>> result;
vector<int> vec;
combine(1,k,n,vec,result);
return result;
}
};
Leetcode题解(4):L216/Combination Sum III的更多相关文章
- LeetCode Combination Sum III
原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...
- [Leetcode 216]求给定和的数集合 Combination Sum III
[题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...
- Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)
Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...
- [LeetCode] 216. Combination Sum III 组合之和 III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Leetcode 39 40 216 Combination Sum I II III
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- 【LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 【刷题-LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- Combination Sum,Combination Sum II,Combination Sum III
39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...
随机推荐
- rails提供的validators
Instance Public methods attribute_method?(attribute)Link Returns true if attribute is an attribute m ...
- 刷题总结——coneology(poj2932 扫描线)
题目: Description A student named Round Square loved to play with cones. He would arrange cones with d ...
- java web项目防止多用户重复登录解决方案
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本人声明.否则将追究法律责任.作者:永恒の_☆ 地址:http://blog.csdn.net/chenghui031 ...
- Java 微信公众号开发--- 接入微信
开发微信公众号在没有正式的公众平台账号时,我们可以使用测试平台账号--- 测试平台申请地址:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandb ...
- 【CF725D】Contest Balloons(贪心,堆)
题意:acm队伍可以得气球,相同气球数是一个排名.每个队伍有一个气球数上限,如果该队伍的气球数大于上限 该队伍被淘汰.给了你队伍的气球数,你的气球可以给别人,问你最大可能的排名. (2 ≤ n ≤ 3 ...
- 【Visual Studio】error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1600”不匹配值“1800” (转)
1.案例一 _MSC_VER 定义编译器的版本.下面是一些编译器版本的_MSC_VER值:MS VC++ 10.0 _MSC_VER = 1600MS VC++ 9.0 _MSC_VER = 1500 ...
- AForge.NET 设置摄像头分辨率
AForge.NET 老版本在预览摄像头时可通过设置DesiredFrameSize 属性,设置摄像头支持的分辨率,新版本提示已过期: 解决办法: 获取VideoCapabilities属性集合,选中 ...
- linux信号------探步
前言 Linux以进程为单位来执行程序.我们可以 将计算机看作一个大楼,内核(kernel)是大楼的管理员,进程是大楼的房客.每个进程拥有一个独立的房间(属于进程的内存空间),而每个房间都是不允 许该 ...
- Python入门--17--pickle
pickle模块,实际用途也很简单.单一,主要用于保存列表(list),元祖(Tuple),字典(dictionary) 当然,一定是大的列表.字典什么的,成千上万行的字典.列表,才能凸显出它的用武之 ...
- 开发使用mysql的一些必备知识点整理(二)查询
简介 查询的基本语法 select * from 表名; from关键字后面写表名,表示数据来源于是这张表 select后面写表中的列名,如果是*表示在结果中显示表中所有列 在select后面的列名部 ...