【leetcode】Combination Sum III(middle)
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)的更多相关文章
- 【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 ...
- 【LeetCode】House Robber III(337)
1. Description The thief has found himself a new place for his thievery again. There is only one ent ...
- 【leetcode】Insertion Sort List (middle)
Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...
- 【leetcode】Repeated DNA Sequences(middle)★
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【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. 思路:不能用 ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 【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 ...
- 【leetcode】Compare Version Numbers(middle)
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
随机推荐
- EF上下文管理
- 随鼠标移动tab
<script language="javascript"> function tabChange(obj, id) { var ...
- php字符串格式化函数addslashes()
1.这个函数的使用和php.ini中的magic_quotes_gpc的配置有关,默认情况下,这个配置为on.并且,这个配置处于一个较高级别,脚本中不能修改.所以,检测这个配置情况就很重要. 2.在脚 ...
- QQ空间个人中心的广告
http://qzonestyle.gtimg.cn/qzone/space_item/boss_pic/*.jpghttp://img*.paipaiimg.com/*.jpghttp://cn.q ...
- Android系统截屏的实现(附代码)
1.背景 写博客快两年了,写了100+的文章,最火的文章也是大家最关注的就是如何实现android系统截屏.其实我们google android_screen_ ...
- Android开源项目(二)
第二部分 工具库 主要包括那些不错的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多媒体相关及其他. 一.依赖注入DI 通过 ...
- mysql不同版本号之间的一些区别
5.1.69-community和5.6.26-log版本相比,有一些语法不支持,如: datetime(3),CURRENT_TIMESTAMP(3)
- cocos2dx中对象的两步初始化
笔者进来开始研究cocos2d这个非常火爆的游戏引擎,在一番折腾后,总算在win7系统下把windows和android平台搭建好了.当然接下来是从官方示例中最简单的HelloCpp项目开始.笔者使用 ...
- Gunicorn 问题
Does Gunicorn suffer from the thundering herd problem? The thundering herd problem occurs when many ...
- 【浅墨Unity3D Shader编程】之一 夏威夷篇:游戏场景的创建 & 第一个Shader的书写
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/40723789 作者:毛星云(浅墨) ...