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]]

思路:

组合问题,递归,1-9 每个数字都分放入和不放入两种情况。得到满足条件的就压入答案。

class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n) {
vector<int> partans;
vector<vector<int>> ans;
combination(, k, n, partans, ans);
return ans;
} void combination(int curNum, int k, int sum, vector<int> &partans, vector<vector<int>> &ans)
{
if(curNum > || sum < || k < ) return; //注意这里是 > 10, 否则数字9的答案无法压入
if(sum == && k == )
{
ans.push_back(partans);
}
else
{
partans.push_back(curNum);
combination(curNum + , k - , sum - curNum, partans, ans);
partans.pop_back();
combination(curNum + , k, sum, partans, ans);
}
}
};

【leetcode】Combination Sum III(middle)的更多相关文章

  1. 【leetcode】Number of Islands(middle)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  2. 【LeetCode】House Robber III(337)

    1. Description The thief has found himself a new place for his thievery again. There is only one ent ...

  3. 【leetcode】Insertion Sort List (middle)

    Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...

  4. 【leetcode】Repeated DNA Sequences(middle)★

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  5. 【leetcode】Balanced Binary Tree(middle)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  6. 【leetcode】Set Matrix Zeroes(middle)

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...

  7. 【leetcode】Spiral Matrix II (middle)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  8. 【leetcode】 search Insert Position(middle)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  9. 【leetcode】Compare Version Numbers(middle)

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

随机推荐

  1. 存储过程获取最后插入到数据表里面的ID

    存储过程获取最后插入到数据表里面的ID SET NOCOUNT on;---不返回影响行数提高性能GOcreate proc [sp_bbs_thread_Insert] @id int output ...

  2. iOS: Crash文件解析(一)

    iOS Crash文件的解析(一) 开发程序的过程中不管我们已经如何小心,总是会在不经意间遇到程序闪退.脑补一下当你在一群人面前自信的拿着你的App做功能预演的时候,流畅的操作被无情地Crash打断. ...

  3. etcd

    https://github.com/silenceper/dcmp http://studygolang.com/topics/1866

  4. android 读取SQLite android could not open the database in read/write mode错误

    由于AndroidManifest.xml文件中uses-permission没有设置权限问题 <uses-permission android:name="android.permi ...

  5. PHP多态的理解

    多态性的一般定义为:同一个操作作用于不同的类的实例,将产生不同的执行结果.也即不同类的对象收到相同的消息时,将得到不同的结果.在实际的应用开发中,采用面向对象中的多态主要在于可以将不同的子类对象都当作 ...

  6. C#入门随手笔记

    1..Net Framework是Microsoft为开发应用程序而创建的一个开发平台. 运行操作系统不限:Microsoft版本运行在windows,Mono版本运行开Linux和MacOS: 应用 ...

  7. MFC中CString转化为char*

    char* convertCStringToChars(CString string) { int nLength=string.GetLength(); ]; memset(c,,nLength+) ...

  8. 在Xcode6.4中使用OpenCV

    XCode版本6.4,OpenCV版本3.0.0 昨天我安装完OpenCV之后,兴奋地按照这篇文章Mac平台上OpenCV开发环境搭建的步骤,在XCode上建了一个Demo工程,结果编译一直不成功.一 ...

  9. linux 中断理解

    1.进程.线程只针对的是应用层,而内核调用.驱动没有这种概念,调用的都是内核调用里相同的函数或变量,所以应用层多个应用操作同个硬件时,特别是要加互斥操作,8250通过cs针脚决定发送数据给哪个串口 2 ...

  10. Android学习笔记(二十)——自定义内容提供器

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 如果我们想要实现跨程序共享数据的功能,官方推荐的方式就是使用内容提供器,可以通过新建一个类去继承 Conten ...