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. spring AOP详解〇

    AOP正在成为软件开发的下一个圣杯.使用AOP,你可以将处理aspect的代码注入主程序,通常主程序的主要目的并不在于处理这些aspect.AOP可以防止代码混乱. 为了理解AOP如何做到这点,考虑一 ...

  2. Linux Shell系列教程之(四)Shell注释

    本文是Linux Shell系列教程的第(四)篇,更多shell教程请看:Linux Shell系列教程 与许多的编程语言一样,Shell中也有注释符号,今天就为大家来介绍下Shell中的注释的语法及 ...

  3. hdu5730 Shell Necklace 【分治fft】

    题目 简述: 有一段长度为n的贝壳,将其划分为若干段,给出划分为每种长度的方案数,问有多少种划分方案 题解 设\(f[i]\)表示长度为\(i\)时的方案数 不难得dp方程: \[f[i] = \su ...

  4. [转] 细说linux挂载——mount

    转载的文章不能分类 这点比较坑   暂时先发到随笔里了 标题会标注的 找到一篇写的对挂载比较好的文章  收藏ing 作者:adagio   Ubuntu社区 原帖网址:http://forum.ubu ...

  5. python hashlib模块 logging模块 subprocess模块

    一 hashlib模块 import hashlib md5=hashlib.md5() #可以传参,加盐处理 print(md5) md5.update(b'alex') #update参数必须是b ...

  6. socket编程-微软小兵

    socket两端建立连接,不断开的连接的情况下做数据交互,客户端发送数据和服务端返回数据.直到客户端要求断开,则关闭连接. 代码目录结构:

  7. 欧拉函数之和(51nod 1239)

    对正整数n,欧拉函数是小于或等于n的数中与n互质的数的数目.此函数以其首名研究者欧拉命名,它又称为Euler's totient function.φ函数.欧拉商数等.例如:φ(8) = 4(Phi( ...

  8. 【NOIP2016】换教室(DP,期望)

    题意: 对于刚上大学的牛牛来说, 他面临的第一个问题是如何根据实际情况中情合适的课程. 在可以选择的课程中,有2n节课程安排在n个时间段上.在第 i ( 1≤ i≤n)个时同段上, 两节内容相同的课程 ...

  9. 【Eclipse】Eclipse中tomcat的Server配置(解决修改代码不断的重启服务器)以及设置tomcat文件发布位置与JSP编译位置查看

     Eclipse有时候修改一点JS或者JSP都会自动重启,有时候修改完JS或者JSP之后必须重启服务器才生效,下面研究了server的一些选项之后彻底解决了这些问题,下面做记录: 我的 Eclipse ...

  10. POJ 1797 Heavy Transportation SPFA变形

    原题链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K T ...