Combinations

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],
]

解法一:递归

递推点:加入i后,下一个加入的元素需要遍历i+1~n

因此可以基于k做递归。

base case: k==cur.size(),此时cur即为符合条件的一个集合。

class Solution {
public:
vector<vector<int> > combine(int n, int k) {
vector<vector<int> > ret;
vector<int> cur;
Helper(ret, cur, k, , n);
return ret;
}
void Helper(vector<vector<int> >& ret, vector<int> cur, int k, int pos, int n)
{
if(cur.size() == k)
ret.push_back(cur);
else
{
for(int i = pos; i <= n; i ++)
{
cur.push_back(i);
Helper(ret, cur, k, i+, n);
cur.pop_back();
}
}
}
};

解法二:非递归

遍历子集过程中,大小为k的子集即为所需集合。

注意略去大小超过k的子集,若不然存储所有子集需要2^n空间。

子集遍历法参考Subsets

class Solution {
public:
vector<vector<int> > combine(int n, int k) {
vector<vector<int> > ret;
vector<vector<int> > subsets;
vector<int> cur; //empty set
//check all subsets with k elements
subsets.push_back(cur);
for(int i = ; i <= n; i ++)
{//all element put into all subsets in ret
int size = subsets.size();
for(int j = ; j < size; j ++)
{
cur = subsets[j];
if(cur.size() >= k)
continue;
cur.push_back(i);
if(cur.size() == k)
ret.push_back(cur);
else
//cur.size() < k
subsets.push_back(cur);
}
}
return ret;
}
};

【LeetCode】77. Combinations (2 solutions)的更多相关文章

  1. 【LeetCode】77. Combinations 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  2. 【一天一道LeetCode】#77. Combinations

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  3. 【leetcode】 Letter Combinations of a Phone Number(middle)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  4. 【leetcode】Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  5. 【LeetCode】18. 4Sum (2 solutions)

    4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d  ...

  6. 【LeetCode】46. Permutations (2 solutions)

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  7. 【LeetCode】49. Anagrams (2 solutions)

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

  8. 【LeetCode】120. Triangle (3 solutions)

    Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...

  9. 【LeetCode】78. Subsets (2 solutions)

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

随机推荐

  1. Codeforces Round #300 B. Quasi Binary 水题

    B. Quasi Binary Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/538/probl ...

  2. memcached添加日志输出

    引子:qa的memcached总是隔一段时间挂掉,导致qa进不去.决定查一下原因,于是添加日志输出,等下次出错便于查阅.定位问题. memcache默认没有日志输出.如果想把memecache服务日志 ...

  3. Programmable current source requires no power supply

    Engineering labs are usually equipped with various power supplies, voltmeters, function generators, ...

  4. IPC low/medium/high density 什么意思?

    http://wiki.altium.com/pages/viewpage.action?pageId=3080344 Land Pattern Information Density Level A ...

  5. android 高清壁纸设置慢

    由于项目的需要最近在解决一个 bug  在1080p 的手机上面设置壁纸会很慢慢,慢的原因是和壁纸 的大小 有关,壁纸越大,时间直越长,一般1080 p 的壁纸大概有10M左右, 所以通过文件流 来保 ...

  6. python笔记18-sort和sorted区别

    前言 python的排序有两个方法,一个是list对象的sort方法,另外一个是builtin函数里面sorted,主要区别: sort仅针对于list对象排序,无返回值, 会改变原来队列顺序 sor ...

  7. 第十四章 openwrt 安装 python

    需要安装libffi,python-mini,python.libffi以及python-mini需要安装在python之前     如果部分软件包不一样可以在下面的web后台搜索,搜索前先opkg ...

  8. (转)SqlServer里DateTime转字符串

    原文:http://www.cnblogs.com/kimbosung/p/4515670.html ), )::: ), ): :::953PM ), ): ), ): ), ): ), ): :: ...

  9. jquery省市选择案例

    1.代码实例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  10. 关于TagHelper的那些事情——自定义TagHelper(格式化输出、依赖注入使用)

    自定义TagHelper的最后一步就是在Process方法或ProcessAsync方法中添加展现代码.熟悉WebControl开发的朋友都知道Render方法,在这个方法中会添加展现的Html元素和 ...