【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 ...
随机推荐
- 利用WCF技术降低系统之间的耦合度
为了降低本系统各个组件之间的耦合度,本系统将BLL层采用WCF技术发布为Web Service,以供UI层调用. 前面我们已经介绍过,为什么UI层不直接调用BLL层,而是要经过UI->Servi ...
- svn还原到指定版本
svn还原到指定版本 1,选中文件夹,右健,show log 2,选中指定版本,右健,Revert to this revision 3,svn commit 4,ok
- hdu.1043.Eight (打表 || 双广 + 奇偶逆序)
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- join 和 union 区别
JOIN和UNION区别 join 是两张表做交连后里面条件相同的部分记录产生一个记录集,union是产生的两个记录集(字段要一样的)并在一起,成为一个新的记录集 .JOIN用于按照ON条件联接两个表 ...
- Javascript高级程序设计——面向对象之实现继承
原型链: 构造函数中都有一个prototype属性指针,这个指针指向原型对象,而创建的实例也有指向这个原型对象的指针__proto__.当实例查找方法时先在实例上找,找不到再通过__proto__到原 ...
- weapp微信小程序初探demo
https://github.com/donglegend/weapp-demo 参考文档开发工具安装微信weapp API git项目源码微信小程序 demo效果展示效果预览
- 【转】 GridView 72般绝技
说明:准备出一个系列,所谓精髓讲C#语言要点.这个系列没有先后顺序,不过尽量做到精.可能会不断增删整理,本系列最原始出处是csdn博客,谢谢关注. C#精髓 第四讲 GridView 72般绝技 作者 ...
- 观察者(Observer)模式
http://www.cnblogs.com/zhenyulu/articles/73723.html 一. 观察者(Observer)模式 观察者模式又叫做发布-订阅(Publish/Subscri ...
- createElement() 创建元素 appendChild()添加元素
Javascript window 对象的document.createElement() 方法.语法及其使用. 1.方法 创建一个新的html元素对象,并可返回一个Element 对象,新创建的El ...
- 破解Excel保护
一.录制宏 二.停止录制 三.查看录制 四.点击编辑进入VB编辑环境 五.清空原有的内容,copy以下代码 Public Sub 工作表保护密码破解() Const DBLSPACE As Strin ...