【leetcode】Combinations (middle)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
思路:有点像0-1背包问题, 对于从1-n的每一个数字都可以选择放入答案 和不放入答案。 当长度达到k时就是一个符合条件的解。
递归的代码,AC了。只要注意状态的还原就好。
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; class Solution {
public:
vector<vector<int> > combine(int n, int k) {
vector<vector<int>> ans;
combinepart(ans, , k, n);
return ans;
} void combinepart(vector<vector<int>> &ans, int num, int k, int n)
{
static int i = ;
static vector<int> partans;
if(num - > n || partans.size() + (n - num + ) < k) return; //数字超过了n 或者即使后面数字全部压入长度也不够的时候 直接返回 避免不必要的计算
if(i == k)
{
ans.push_back(partans);
return;
} partans.push_back(num); //放入num
i++;
combinepart(ans, num + , k, n); partans.pop_back();
i--;
combinepart(ans, num + , k, n);//不放入num
}
}; int main()
{
Solution s;
vector<vector<int>> ans = s.combine(,); return ;
}
网上有非递归的代码,可是我好困,懒得看... 速度都差不多的,因为我的递归截枝了,没有多余的操作。
class Solution {
public:
vector<vector<int> > combine(int n, int k) {
vector<vector<int> >ret;
if(k>n||k<=)return ret; for(int i=;i<=n-k+;i++){
ret.push_back(vector<int>(,i));
} for(int i=;i<=k;i++){
int num=ret.size();
for(int j=;j<num;j++){
int last=ret[j].back();
vector<int> pretmp=ret[j];
ret[j].push_back(last+);
for(int p=last+;p+k-i<=n;p++){
vector<int> tmp=pretmp;
tmp.push_back(p);
ret.push_back(tmp);
}
}
} return ret;
}
};
【leetcode】Combinations (middle)的更多相关文章
- 【LeetCode】876. Middle of the Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用哑结点 不使用哑结点 日期 题目地址:https ...
- 【Leetcode】Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 【leetcode】Permutations (middle)
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 【leetcode】Anagrams (middle)
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- 【LeetCode】518. Coin Change 2 解题报告(Python)
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- 绿书模拟day10 单词前缀
[题目描述]一组单词是安全的,当且仅当不存在一个单词是另一个单词的前缀,这样才能保证数据不容易被误解,现在你手上有一个单词集合s,你需要计算有多少个自己是安全的.注意空集永远是安全的.[输入格式]第一 ...
- DatePicker隐藏年月日
1.隐藏年 ((ViewGroup) (((ViewGroup) dp.getChildAt(0)).getChildAt(0))) .getChildAt(0).setVisibility(View ...
- SQL获取日期格式
) + ' ' + CONVERT(VARCHAR, DATEPART(hh, GetDate())) + ':' + ) AS Date ),) ),) ),) as DateTime) as [D ...
- xcode7 The operation couldn't be completed.
问题描述:当运行Xcode6时,编译代码成功,但是登陆模拟器失败,显示错误:The Operation couldn't be completed.(LaunchServicesError error ...
- webrtc第二篇 聊天室
聊天室模型不一样考虑的问题也不一样 1.websocket文本聊天 * step1 : 向聊天室所有用户(不包括该用户自己)发送当前用户上线信息.客户端用户栏回添加此用户 * step2 : 将该用户 ...
- compareTo(String str)与compareToIgnoreCase(String str)
一.compareTo(String str)方法 返回值:如果参数字符串等于此字符串,则返回值 0:如果此字符串按字典顺序小于字符串参数,则返回一个小于 0 的值:如果此字符串按字典顺序大于字符串参 ...
- javascript高级程序设计---Event对象
事件是一种异步编程的实现方式,本质上是程序各个组成部分之间传递的特定消息. DOM的事件操作(监听和触发),都定义在EventTarget接口 该接口就是三个方法,addEventListener和r ...
- nginx 虚拟主机
基于域名的虚拟主机 创建站点目录 [root@nginx conf]# cd /usr/local/nginx/html/ [root@nginx html]# pwd /usr/local/ngin ...
- iOS开发——高级篇——内存分析,Instruments
一.内存分析 1.静态内存分析(Analyze)不运行程序,直接对代码进行内存分析,查看代码是否有内存泄露优点:分析速度快,并且可以对所有的代码进行内存分析缺点:分析结果不一定准确(没有运行程序,根据 ...
- Mean Shift Tracking: 2000-2012回顾 (新论文更新)
参考: Mean Shift Tracking: 2000-2012回顾 (新论文更新) ECCV2016要来了,估计深度学习要一统天下了吧