【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 ...
随机推荐
- [译]git reset
git reset 如果说git revert是一个安全的撤销方式, 那么git reset就是一个非常危险的方法了. 当你使用git reset撤销的时候, 你没有可能在回到最初了-他是一个永久的不 ...
- 繁华模拟赛day8 字典序
/* 这个题要我们求一个字典序,字符串给出的顺序,会对字母的字典序前后相对顺序进行限定,如何用来表示这种限定,我们注意到这种一个之后接着一个,只有先输出他前面的才能输出他,很明显就是拓扑排序,最小方案 ...
- poj3070 Fibonacci
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- SpringMVC 表单标签
引入标签库 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" ...
- UI第四节——UIImageView详解
- (void)viewDidLoad { // super调用是必须的 [super viewDidLoad]; UIImage *image = [UIImage imageNamed:@&quo ...
- 大数据之sqoop
一.安装: 1 解压然后把mysql的驱动放在$SQOOP_HOME/lib 目录中2. conf/sqoop-en.sh export HADOOP_COMMON_HOME=/home/hadoop ...
- 值得订阅的Android 开发者博客
链接:http://www.zhihu.com/question/19788650/answer/60771437来源:知乎 Google 官方[Android Developers Blog](An ...
- 中文的加密传输(python版)
信息传输过程中,可能会被各种监听. 这里介绍一种简单的加密算法(可逆). 正向加密: 字符串 -> 字节(char->int转换) -> 异或每个字节某个KEY ->字节(i ...
- ubuntu 下mongodb安装
1.下载: mongodb.org/download 2. 将下载的压缩文件加压到/usr/lib下 3. 建立软链接 ln -s /usr/lib/mongodb-linux-i686-2.6.7/ ...
- Tomcat异常 Multiple Contexts have a path of "/qqshl".解决方法
Tomcat异常 Multiple Contexts have a path of "/qqshl".解决方法 找到tomcat映射文件Service.xml,将文件中的conte ...