【LeetCode】77. Combinations (2 solutions)
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)的更多相关文章
- 【LeetCode】77. Combinations 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 【一天一道LeetCode】#77. Combinations
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- 【leetcode】 Letter Combinations of a Phone Number(middle)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 【leetcode】Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- 【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 ...
- 【LeetCode】46. Permutations (2 solutions)
Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...
- 【LeetCode】49. Anagrams (2 solutions)
Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...
- 【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 ...
- 【LeetCode】78. Subsets (2 solutions)
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
随机推荐
- 模型的性能评估(二) 用sklearn进行模型评估
在sklearn当中,可以在三个地方进行模型的评估 1:各个模型的均有提供的score方法来进行评估. 这种方法对于每一种学习器来说都是根据学习器本身的特点定制的,不可改变,这种方法比较简单.这种方法 ...
- 发展中的生命力——Leo鉴书69
接触<寻路中国>是在2011年11月24号的正略读书会上.当期主讲嘉宾是万圣书园创始人刘苏里,也是著名的大书评人.读书会有个传统就是每期推荐一本书.当期推荐就是<寻路中国>.事 ...
- 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& ...
- spring整合mybatis是如何配置事务的?
作者:郭无心链接:https://www.zhihu.com/question/30206875/answer/84675373来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- C# 中的回车换行符
在 C# 中,我们用字符串 "\r\n" 表示回车换行符. string str = "第一行\r\n第二行"; 但是我们更推荐 Environment.New ...
- cadence学习(1)常规封装的建立
1.建立焊盘. (1)首先要获得datasheet(或可用pcb matrix ipc-7531标准的可查询封装软件)中元器件的封装信息. (2)建立.pad文件.打开PCB Editor Utili ...
- 【资料】wod烟草
注意: 1. 所有效果持续时间是整个地城 2. 某几样菸草在使用 烟雾的祝福 的时候效果只有LV1 (技能 -25), 表示该物品设计上主要是自己使用而非加给团队. SL = 技能等级 HL = 英雄 ...
- Workflow:采用坐标变换(移动和旋转)画箭头
背景 流程设计器的连线部分需要画一个箭头代表连接的方向,下图是期望的效果: 刚开始我准备采用三角函数(sin和cos)来计算三角的坐标,实现的过程真不爽(有兴趣的朋友可以试试),就在完工的时候,突然想 ...
- oracle全文索引的创建和使用
整理一下我所遇到过的有关全文索引的问题吧 一.设置词法分析器 Oracle实现全文检索,其机制其实很简单.即通过Oracle专利的词法分析器(lexer),将文章中所有的表意单元(Oracle 称为 ...
- 数学图形(2.6)Satellit curve
这曲线有点像鼓,绕在球上两头是开口的. #http://www.mathcurve.com/courbes3d/satellite/satellite.shtml vertices = t = to ...