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. vue-cli + webpack 多页面实例应用

    关于vue.js vue.js是一套构建用户界面的 轻型的渐进式前端框架.它的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.使用vue可以给你的开发带来极致的编程体验. 关于vu ...

  2. R-处理数据对象的实用函数

  3. Excel 使用宏批量修改单元格内指定文字为红字

    -> step 1:新建宏,进入编辑,使用如下代码: Sub Ss()Dim c As RangeFor Each c In ActiveSheet.UsedRange i = 1 While ...

  4. src 小心得

    关于src的引用,不要用相对路径,  ../   虽然省事,但是跳转页面时容易出错. 举个例子: 在web页面引用  D:\phpStudy\WWW\ueditor\utf8-php 这个文件夹下面  ...

  5. 第四天 rxcocoa

    HackerNewsReaderDemo HackerNewsAPI.sharedApi.newStories() .observeOn(ConcurrentDispatchQueueSchedule ...

  6. redis-key2

    package com.ztest.redis; import java.util.List; import redis.clients.jedis.Jedis; import com.sun.ist ...

  7. phpcms中常用代码总结

    1.调用数据库模型 $this->db = pc_base::load_model('test_model');//从"phpcms/model/"目录下加载模型类文件 其中 ...

  8. BPMN流程图的绘制的注意要点

    1.分支网关的表达式,是在选择的线上设置. 2.在分支网关上,可以设置一个默认线的id. 3.并行网关,必须有开始,有结束.

  9. ubuntu安装python一些安装包

    sudo apt-get install python-pip sudo pip install distribute sudo pip install nose sudo pip install v ...

  10. UITabBarController的使用和坑

    本人apem 说到UITabBarController, 最首要的坑就是tabbar的图片不显示的问题 1. tabbar上的图片一定要2套以上, 例如一个uitabbaritem的image是 se ...