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:

  1. [
  2. [2,4],
  3. [3,4],
  4. [2,3],
  5. [1,2],
  6. [1,3],
  7. [1,4],
  8. ]

解法一:递归

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

因此可以基于k做递归。

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

  1. class Solution {
  2. public:
  3. vector<vector<int> > combine(int n, int k) {
  4. vector<vector<int> > ret;
  5. vector<int> cur;
  6. Helper(ret, cur, k, , n);
  7. return ret;
  8. }
  9. void Helper(vector<vector<int> >& ret, vector<int> cur, int k, int pos, int n)
  10. {
  11. if(cur.size() == k)
  12. ret.push_back(cur);
  13. else
  14. {
  15. for(int i = pos; i <= n; i ++)
  16. {
  17. cur.push_back(i);
  18. Helper(ret, cur, k, i+, n);
  19. cur.pop_back();
  20. }
  21. }
  22. }
  23. };

解法二:非递归

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

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

子集遍历法参考Subsets

  1. class Solution {
  2. public:
  3. vector<vector<int> > combine(int n, int k) {
  4. vector<vector<int> > ret;
  5. vector<vector<int> > subsets;
  6. vector<int> cur; //empty set
  7. //check all subsets with k elements
  8. subsets.push_back(cur);
  9. for(int i = ; i <= n; i ++)
  10. {//all element put into all subsets in ret
  11. int size = subsets.size();
  12. for(int j = ; j < size; j ++)
  13. {
  14. cur = subsets[j];
  15. if(cur.size() >= k)
  16. continue;
  17. cur.push_back(i);
  18. if(cur.size() == k)
  19. ret.push_back(cur);
  20. else
  21. //cur.size() < k
  22. subsets.push_back(cur);
  23. }
  24. }
  25. return ret;
  26. }
  27. };

【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. 模型的性能评估(二) 用sklearn进行模型评估

    在sklearn当中,可以在三个地方进行模型的评估 1:各个模型的均有提供的score方法来进行评估. 这种方法对于每一种学习器来说都是根据学习器本身的特点定制的,不可改变,这种方法比较简单.这种方法 ...

  2. 发展中的生命力——Leo鉴书69

    接触<寻路中国>是在2011年11月24号的正略读书会上.当期主讲嘉宾是万圣书园创始人刘苏里,也是著名的大书评人.读书会有个传统就是每期推荐一本书.当期推荐就是<寻路中国>.事 ...

  3. CMSIS-SVD Schema File Ver. 1.1 (draft)

    http://www.keil.com/pack/doc/cmsis/svd/html/group__schema__1__1__gr.html <?xml version="1.0& ...

  4. spring整合mybatis是如何配置事务的?

    作者:郭无心链接:https://www.zhihu.com/question/30206875/answer/84675373来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  5. C# 中的回车换行符

    在 C# 中,我们用字符串 "\r\n" 表示回车换行符. string str = "第一行\r\n第二行"; 但是我们更推荐 Environment.New ...

  6. cadence学习(1)常规封装的建立

    1.建立焊盘. (1)首先要获得datasheet(或可用pcb matrix ipc-7531标准的可查询封装软件)中元器件的封装信息. (2)建立.pad文件.打开PCB Editor Utili ...

  7. 【资料】wod烟草

    注意: 1. 所有效果持续时间是整个地城 2. 某几样菸草在使用 烟雾的祝福 的时候效果只有LV1 (技能 -25), 表示该物品设计上主要是自己使用而非加给团队. SL = 技能等级 HL = 英雄 ...

  8. Workflow:采用坐标变换(移动和旋转)画箭头

    背景 流程设计器的连线部分需要画一个箭头代表连接的方向,下图是期望的效果: 刚开始我准备采用三角函数(sin和cos)来计算三角的坐标,实现的过程真不爽(有兴趣的朋友可以试试),就在完工的时候,突然想 ...

  9. oracle全文索引的创建和使用

    整理一下我所遇到过的有关全文索引的问题吧 一.设置词法分析器 Oracle实现全文检索,其机制其实很简单.即通过Oracle专利的词法分析器(lexer),将文章中所有的表意单元(Oracle 称为  ...

  10. 数学图形(2.6)Satellit curve

    这曲线有点像鼓,绕在球上两头是开口的. #http://www.mathcurve.com/courbes3d/satellite/satellite.shtml vertices = t = to ...