【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-Linq将查询结果转换为List<string>
List<int> id_list = new List<int>() { 1 };//测试数据...List<string> guid_list = (from ...
- 如何打开xip格式的xcode安装包
解决方法如下: 1.保证存储空间 20G 2.去除解压验证 xattr -d com.apple.quarantine Xcode_8_beta.xip 3.双击解压 详见: 从官网下载的 xcode ...
- Swift2.1 语法指南——可空链式调用
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...
- [原创]svn 常见错误总结
错误: Unable to make name in 'X:\nfs\drivers\can_uart\.svn\tmp' 解决: 改变当前文件夹的权限 linux 下显示修改的文件名 参考链接:ht ...
- MySQL Python教程(3)
Class cursor.MySQLCursor 具体方法和属性如下:Constructor cursor.MySQLCursorMethod MySQLCursor.callproc(procnam ...
- 批处理快速更改ip地址
在各种网络中切换,windows更换ip地址步骤: 进入控制面板--网络和internet--网络和共享中心--理性适配器设置--然后找到网卡--进入属性--然后internet 协议--更改ip信 ...
- CMake Error: your CXX compiler: "" was not found
[root@amax src]# cmake . -- The CXX compiler identification is unknown CMake Error at /usr/local/sha ...
- linux shell脚本常用语句
linux shell 指令 诸如-d, -f, -e之类的判断表达式: 文件比较运算符-e filename 如果 filename存在,则为真 [ -e /var/log/syslog ]-d ...
- 一个IP多个https站点配置
在一台主机上放置一个https网站,究竟该怎么配置?对于IIS和Apache以及Nginx等不同的服务器,方法是不同的,网上有很多教程,在此就不再赘述了. 至于一台主机,如何配置多个https网站呢? ...
- PL/Cool
毛子 2003 Petrozavodsk, Final Contest, 8.30.03. G. PL/Cool 实现一个程序,使它读入一段PL/Cool程序,并输出它的结果. PL/Cool语法 b ...