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)的更多相关文章

  1. 【LeetCode】876. Middle of the Linked List 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用哑结点 不使用哑结点 日期 题目地址:https ...

  2. 【Leetcode】Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  3. 【leetcode】Permutations (middle)

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  4. 【leetcode】Anagrams (middle)

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...

  5. 【LeetCode】518. Coin Change 2 解题报告(Python)

    [LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. EasyUI datagrid优化

    easyui datagrid 在IE上加载速度慢, 150行数据就无法忍受了. firefox加载速度还可以. jquery easyui datagrid使用参考 http://www.cnblo ...

  2. WP8.1下 Cortana语音命令 VCD文件 设计

    Windows Phone8.1下的Cortana,可以通过语音的方式,打开.设置应用,进行页面跳转.执行任务. 我们先要创建VCD(VoiceCommand.xml)文件 <?xml vers ...

  3. java实现文件单词频率统计 topN top K

    java 实现单词计数.top N 思路 先统计每个单词出现的个数 利用 TreeSet 的自动排序的功能 上代码 wordcount public void wordCount() { String ...

  4. JavaScript的面临的9个陷阱

    虽然不是什么很高深的技术问题,但注意一下,会使您的编程轻松些,即所谓make life easier. 笔者对某些陷阱会混杂一些评点. 1.   最后一个逗号 如这段代码,注意最后一个逗号,按语言学角 ...

  5. Android学习笔记(十七)——数据库操作(下)

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 这一次我们来试一试升级数据库,并进行数据库的CRUD操作,其中, C 代表添加(Create) ,R 代表查询 ...

  6. Oracle sysdate 时间加减

    加法 select sysdate,add_months(sysdate,12) from dual;        --加1年 select sysdate,add_months(sysdate,1 ...

  7. 4.了解AngularJS模块和依赖注入

    1.模块和依赖注入概述 1.了解模块 AngularJS模块是一种容器,把代码隔离并组织成简洁,整齐,可复用的块. 模块本身不提供直接的功能:包含其他提供功能的对象的实例:控制器,过滤器,服务,动画 ...

  8. 获取Spring容器Bean

    WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); ManageResolver mr = (Ma ...

  9. AFNetworking 2.5.0版本的使用

    http://www.mamicode.com/info-detail-477899.html AFNetworking 2.5.0版本的使用 http://afnetworking.com/ htt ...

  10. win7桌面背景地址

    C:\Users\你的用户名\AppData\Local\Microsoft\Windows\Themes\Alaskan L\DesktopBackground