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. 更新yum源/apt-get源

    国内开源镜像站有:网易: http://mirrors.163.com/ 搜狐: http://mirrors.sohu.com/阿里云: http://mirrors.aliyun.com/北京理工 ...

  2. 使用ScrapySharp快速从网页中采集数据

    ScrapySharp是一个帮助我们快速实现网页数据采集的库,它主要提供了如下两个功能 从Url获取Html数据 提供CSS选择器的方式解析Html节点 安装: ScrapySharp可以直接从Nug ...

  3. 升/降压转换器 (Buck-boost)

    升/降压转换器 (Buck-boost) 当输入电压是变动的,有时比输出电压高.有时却比较低时(例如放电中的电池),升/降压转换器 (Buck-boost) 是最佳的电源解决方案. 升/降压转换器 ( ...

  4. AskUsingForm_c函数

    IDA SDK里面提供的UI(user interface)函数 AskUsingForm_c,该函数弹出一个对话框,而对话框的外观形式,就由此函数的第一个参数form(const char *类型) ...

  5. /etc/fstab 解析

    http://www.codesec.net/view/39930.html root@qs-wg-db1 /]# cat /etc/fstab LABEL=/ / ext3 defaults 1 1 ...

  6. asp动态数组

    本文所说的 ASP 数组是指在 ASP 中以默认语言 VBScript 为语言的数组. 样例: Dim   MyArray()        for   i   =   0   to   10    ...

  7. .NET(C#)主流的ORM框架

    .NET(C#)主流ORM总揽 SqlSugar (国内) Dos.ORM (国内) Chloe (国内) StackExchange/Dapper (国外) Entity Framework (EF ...

  8. 高级需求分析UML建模设计模式笔记

    1.REQ->HLR 分析 全系统性质->AD设计 Context,BOM,Conception 2.REQ->LLR 分析 模块分析->DD设计 + 编码 Feature,B ...

  9. mvn sonar:sonar在jenkins步骤的执行位置影响执行结果

    1.如图所示,sonar执行可以在build中执行,也可以在步骤Post Steps中执行(mvn sonar:sonar) 2.但是在步骤Post Steps中执行的话,有一个问题,就是假如项目有多 ...

  10. iOS:切换视图时,反向传递数据方法一:通知

    通知方式: 1.有一个(单例)通知中心,负责管理iOS中的所有通知 2.需要获取某种通知,必须注册成为观察者(订阅) 3.不再需要取某种通知时,要取消注册. 4.你可以向通知中心发送某种通知,通知中心 ...