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. ida sdk add_struc_member array

    tid_t tid = get_struc_id ( "foo_type" ) ; struc_t * sptr = get_struc ( tid ); if ( sptr == ...

  2. CMSIS-SVD Schema File Ver. 1.0

    <?xml version="1.0" encoding="UTF-8"?> <!-- date: 07.12.2011 Copyright ...

  3. Visual Studio删除所有的注释和空行

    visual studio用"查找替换"来删掉源代码中所有//方式的纯注释和空行 注意:包括/// <summary>这样的XML注释也都删掉了. 步骤1/2(删除注释 ...

  4. Asp.Net Session的三种方法及Web.Config设置

    转载:http://user.gw-ec.com/login/safelog/redirectt?session=so%2f%2bSjyZURMOe54xgk%2bUhL2CgGqDjOKEbYwZS ...

  5. Appium+python自动化10-AVD 模拟器

    前言 有些小伙伴没android手机,这时候可以在电脑上开个模拟器玩玩 一.模拟器配置 1.双击启动AVD Manager,进入配置界面

  6. hdu1269迷宫城堡 (强连通Tarjan+邻接表)

    Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每一个通道都是单向的,就是说 ...

  7. Ubuntu12.04 挂载exFat格式U盘的方法(转)

    原文链接:Ubuntu12.04 挂载exFat格式U盘的方法 首先关于exFAT ,这里就不多作解释了,   再介绍一个软件fuse-exfat,   https://code.google.com ...

  8. (高精度运算4.7.24)UVA 10013 Super long sums(大数加法——某一位的数字可能大于10)

    /* * UVA_10013.cpp * * Created on: 2013年10月29日 * Author: Administrator */ #include <iostream> ...

  9. django基础复习

    Django - 路由系统 url.py - 视图函数 views.py - 数据库操作 models.py - 模板引擎渲染 - HttpReponse(字符串) - render(request, ...

  10. [Angular] Angular Elements Intro

    Make sure install the latest Angular v6 with Angular CLI. Checkout ght Github for the code. 1. Creat ...