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的更多相关文章

  1. LeetCode Combination Sum III

    原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...

  2. [Leetcode 216]求给定和的数集合 Combination Sum III

    [题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...

  3. Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)

    Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...

  4. [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 ...

  5. 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 ...

  6. 【LeetCode】216. Combination Sum III

    Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...

  7. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  8. 【刷题-LeetCode】216. Combination Sum III

    Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...

  9. 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 ...

随机推荐

  1. [转]如何把嵌套的python list转成一个一维的python list?

    import itertools a = [[1,2,3],[4,5,6], [7], [8,9]] out = list(itertools.chain.from_iterable(a))

  2. hdu1595 最短路问题(dijkstra&&spfa)

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  3. Thinkphp5.1手册太简单,有的功能用起来不确定结果是否和预料的一样,顾整理记录

    //模板{if false} 1 {else/} //====>可以使用 效果同 {else /} 2 {/if} {if condition="(1 eq 1) and false& ...

  4. HDU-2413 Against Mammoths

    二分答案,对于当前答案Ans,求出某些人类可打败某些外星人的对应边,建图后求是否有完备匹配. //#include <cmath> #include <cstdlib> #in ...

  5. 【bzoj1053】[HAOI2007]反素数ant

    对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4.如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数.例如,整数1,2,4,6等都是反质数 ...

  6. 洛谷P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers

    题目描述 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少.在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人.然而,在任何一群朋友中 ...

  7. 标准C程序设计七---47

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  8. Python入门--20--类、对象

    OO=Object Oriented 面向对象 python是面向对象的编程语言 OO的特征: 1.封装:把一堆东西都扔到一起,变为一个类 2.继承:假如一个类里面 3.多态:不同的类有相同名称的函数 ...

  9. hdu 3605 /状态合并最大流

    题意:N个人去m个星球,给出n个人可以去哪些星球的01矩阵.求是否能满足所有人都去.(n到10万,m<=10) 一看,起先一瞬间就建图,准备秒了,人向星球连边,直接最大流判断是否为n,提交超时. ...

  10. LaTeX 如何在文档的侧面插入图片实现"绕排"?

    https://www.zhihu.com/question/26837705 https://blog.csdn.net/u012856866/article/details/70305834