【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 ...
随机推荐
- Codeforces Round #302 (Div. 2) B. Sea and Islands 构造
B. Sea and Islands Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544/p ...
- SQL 语句实现排序问题!
SQL 查询数据时按某列排序后增加排名列,需排名的列值相等时排名相同,即如需排名列数组为:9,9,8,7,7,6 添加的排名列数组需要显示为两种: 第一种:1,1,3,4,4,6 (这种排名方 ...
- JSP页面乱码全解析
乱码指的是中文乱码. 一.POST乱码 由于两个页面都是用UTF-8编码,但是参数的传输默认是ISO-8859-1,这时候可以在接受参数之前 request.setCharacterEncoding( ...
- unidac记录日志
unidac记录日志 1)SQL日志记录 TUniSqlMonitor的OnSql事件里面记录SQL日志,演示代码如下: procedure TfrmDB.UniSQLMonitor1SQL(Send ...
- 为什么要用<!DOCTYPE>声明
实例: 我们常常会看到类似这种代码: <!DOCTYPE html> <html> <head> <title>文档的标题</title> ...
- unity3D总结的一些细节,不注意有些要折腾非常多天!
1. 注意!!ps保存图片时,若保存为ps格式,若关闭最大兼容将会导致unity导入失败!(n天) 2.switch 推断NGUI popuplist传来的value字符串时一定要trim一下去掉空格 ...
- Integer IntegerCache源码
先看一段测试结果: /*public static void main(String[] args) { Integer a = 128, b = 128; Integer c = 127, d = ...
- Python实现MapReduce,wordcount实例,MapReduce实现两表的Join
Python实现MapReduce 下面使用mapreduce模式实现了一个简单的统计日志中单词出现次数的程序: from functools import reduce from multiproc ...
- python的__slots__节约内存的魔法;检查python每一行代码内存占用情况的工具
在Python中,每个类都有实例属性.默认情况下Python用一个字典来保存一个对象的实例属性.这非常有用,因为它允许我们在运行时去设置任意的新属性. 然而,对于有着已知属性的小类来说,它可能是个瓶颈 ...
- apache kafka监控系列-KafkaOffsetMonitor(转)
原文链接:apache kafka监控系列-KafkaOffsetMonitor 概览 最 近kafka server消息服务上线了,基于jmx指标参数也写到zabbix中了,但总觉得缺少点什么东西, ...