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. CSS文档流

    文档流 将窗体自上而下分成一行行, 并在每行中按从左至右的顺序排放元素,即为文档流. 每个非浮动块级元素都独占一行, 浮动元素则按规定浮在行的一端. 若当前行容不下, 则另起新行再浮动. 内联元素也不 ...

  2. 使用SandCastle创建.Net帮助文档

    使用SandCastle创建.Net帮助文档 引用自:http://www.cnblogs.com/DotNetNuke/archive/2009/04/23/1441899.html Sandcas ...

  3. KVM虚拟机内存不足,调整参数

    Dec :: vgfs001 kernel: tiotest_AMD_x86 invoked oom-killer: gfp_mask=, oom_adj=, oom_score_adj= Dec : ...

  4. 增值税——基础知识

    一.增值税的概念 增值税是对从事销售货物或者提供加工.修理修配劳务以及从事进出口货物的单位和个人取得的增值额为课税对象征收的一种税. 增值额是指纳税人在生产.经营或劳务活动中所创造的新增价值,即纳税人 ...

  5. 【bzoj1596】[Usaco2008 Jan]电话网络

    题目描述 Farmer John决定为他的所有奶牛都配备手机,以此鼓励她们互相交流.不过,为此FJ必须在奶牛们居住的N(1 <= N <= 10,000)块草地中选一些建上无线电通讯塔,来 ...

  6. 推荐系统(协同过滤,slope one)

    1.推荐系统中的算法: 协同过滤: 基于用户 user-cf 基于内容 item –cf slop one 关联规则 (Apriori 算法,啤酒与尿布) 2.slope one 算法 slope o ...

  7. Mac Pro 入门、遇到的问题、个性化设置 汇总

    入门资料 入门一:Mac 基本用法 入门二:Mac 使用VMware Fusion虚拟机 入门三:Mac 使用brew安装软件 问题汇总 [问题1]如何复制文本? 一只手指头按下,另外一只手指头滑动选 ...

  8. vue.js 简单入门

    转载自:http://blog.csdn.net/violetjack0808/article/details/51451672 <!DOCTYPE html> <html lang ...

  9. 机器学习中的Bias(偏差),Error(误差),和Variance(方差)有什么区别和联系?

    前几天搜狗的一道笔试题,大意是在随机森林上增加一棵树,variance和bias如何变化呢? 参考知乎上的讨论:https://www.zhihu.com/question/27068705 另外可参 ...

  10. (原创)Activity启动模式之singleTask

    android中activity有四种启动模式 standard(默认):在同一个任务栈中可以有重复的activity,基本没什么限制 singleTop:只有当此activity在栈顶时,去创建它, ...