【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 ...
随机推荐
- linux压缩排除
tar -zcvf www/la.tar.gz --exclude=www/uploadfile --exclude=www/databases --exclude=www/web_logs www ...
- 学习(主题或切入点)checklist1
业务+技术+架构+运维+管理 技术学习:http://www.runoob.com/mongodb/mongodb-query.html 一.技术篇补充学习列表 1,mongodb(o) 2,red ...
- call() 和 apply() ----预定义的函数方法
- Thank you for your resubmission. Performance - 2.3.10 We noticed that your app or its metadata includes irrelevant third-party platform information. Specifically, Android logo is mentioned in the
被拒很多次,各种修改,最后发现是提交的时候,含有安卓的图标!欲哭无泪呀! Thank you for your resubmission. Performance - 2.3.10 We notice ...
- 【GXZ的原创】C++小游戏——五子棋
前些时候考完试自己编的带有胜负判定的五子棋. 操作方法:WSAD或↑↓←→移动下棋位置,Space或Enter放置. 如果游戏出现bug,欢迎大家在评论区反馈. #include <stdio. ...
- Ubuntu 12 修改当前用户密码:new password is too simple
修改当前登录用户的密码,通常使用如下命令: $ passwd Old password:****** New password:******* Re-enter new password:****** ...
- Mac Mini中添加VNC访问
开启Mac Mini上面的VNC. 1) 打开“系统偏好设置”(System Preference),双击打开“共享”(Sharing)项. 2)在左侧将“屏幕共享”(Screen sharing) ...
- 关于viewport
最近无聊的很,买了本教材,学习响应式网站设计. 因为有多年css的编程基础,前面的媒介查询学的很顺利.当学到viewport这个mata标签的时候,教程讲的比较简单. 今天,百度了不少资料,基本搞清楚 ...
- 雪峰配置的nginx
- Java Io 流(输入输出流)
IO流,也就是输入和输出流,可分为字节流和字符流. 1. 字节流 (1). InputStream 输入流,用于读取文件 输入流常用API: inputStream.read() 读取一个字节 in ...