【Combinations】cpp
题目:
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],
]
代码:
class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int> > ret;
if ( n<k ) return ret;
vector<int> tmp;
Solution::dfs(ret, , k, n, ,tmp);
return ret;
}
static void dfs(vector<vector<int> >& ret, int index, int k, int n, int step, vector<int>& tmp)
{
if ( step==k )
{
ret.push_back(tmp);
return;
}
for ( size_t i = index; i <=n; ++i )
{
tmp.push_back(i);
Solution::dfs(ret, i+, k, n, step+, tmp);
tmp.pop_back();
}
}
};
tips:
dfs解法。
几个参数的含义:
ret: 存储返回combination结果
index: 当前起始元素是多大
k,n:与题意所给相同
step:深入到第几层
思路:dfs的每层增加一个元素,直到深度等于k返回结果;如果从index开始的元素不足以打到深度k,则不会添加到ret中。
======================================
其实,可以不用传入6个参数,传入5个足以。代码如下:
class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int> > ret;
if ( n<k ) return ret;
vector<int> tmp;
Solution::dfs(ret, , k, n, tmp);
return ret;
}
static void dfs(vector<vector<int> >& ret, int index, int k, int n, vector<int>& tmp)
{
if ( tmp.size()==k )
{
ret.push_back(tmp);
return;
}
for ( size_t i = index; i <=n; ++i )
{
tmp.push_back(i);
Solution::dfs(ret, i+, k, n, tmp);
tmp.pop_back();
}
}
};
用tmp.size()代替step,代码跟简洁一些,因此更推崇后一种方法。
=======================================
leetcode写到这里有些感觉了,搜了一下dfs bfs的模板相关的内容,基本跟实战中的经验差不多:
http://blog.csdn.net/fightforyourdream/article/details/12866861
http://www.cnblogs.com/HectorInsanE/archive/2010/11/09/1872656.html
=======================================
再补上一种迭代的解法,如下:
class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int> > ret;
// corner cases
if ( n<k || k== ) return ret;
// initialization
for ( int i = ; i < n-k+; ++i )
{
vector<int> tmp;
tmp.push_back(i+);
ret.push_back(tmp);
}
// combines
for ( int i = ; i < k-; ++i )
{
vector<vector<int> > tmp = ret;
ret.clear();
for ( int j = ; j < tmp.size(); ++j )
{
int curr = tmp[j].back();
for ( int v = curr+; v <= n; ++v )
{
vector<int> ori = tmp[j];
ori.push_back(v);
ret.push_back(ori);
}
}
}
return ret;
}
};
tips:
1. 这种迭代方式返回的组合内部都是由小到大排序的。
2. intialization的含义是”组合首个元素最小到最大“(比如n=4,k=2 按照原则1,则首个元素最大取到3,因为比4小的元素没有了不足以满足k=2的条件)
3. 每轮迭代,先取出来当前组合最后一个元素(最大的)curr;再从curr+1到n挨个取值,补到tmp[j]的后面,构成新的组合。
4. 控制迭代的次数是k-1次(因为initialization已经算了一次)
===============================================
第二次过这道题,用dfs过的,传入参数比第一次过更简洁一些。
class Solution {
public:
vector<vector<int> > combine(int n, int k)
{
vector<vector<int> > ret;
vector<int> tmp;
Solution::dfs(ret, tmp, n, k);
return ret;
}
static void dfs(
vector<vector<int> >& ret,
vector<int>& tmp,
int n, int k)
{
if ( tmp.size()==k )
{
ret.push_back(tmp);
return;
}
int index = tmp.empty() ? : tmp.back();
for ( int i=index+; i<=n; ++i )
{
tmp.push_back(i);
Solution::dfs(ret, tmp, n, k);
tmp.pop_back();
}
}
};
【Combinations】cpp的更多相关文章
- 【Permutations】cpp
题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...
- 【Subsets】cpp
题目: Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset ...
- 【Anagrams】 cpp
题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...
- 蓝桥杯 【dp?】.cpp
题意: 给出一个2*n的方格,当刷完某一个方格的漆后可以且只可以走到相邻的任何一格,即上 下 左 右 左上 左下 右上 右下.可以从任意一个格子开始刷墙,问有多少种刷法,因为随着n的增大方案数会变多, ...
- 【Triangle 】cpp
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- 【N-Queens】cpp
题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...
- 【Candy】cpp
题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...
- 【4Sum】cpp
题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- 【3Sum】cpp
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...
随机推荐
- 零基础逆向工程26_C++_03_Vector
1 Vector 核心代码 #define SUCCESS 1 // 成功 #define ERROR -1 // 失败 #define MALLOC_ERROR -2 // 申请内存失败 #defi ...
- koa源码分析
最近项目要使用koa,所以提前学习一下,顺便看了koa框架的源码. 注:源码是koa2.x koa的源码很简洁,关键代码只有4个文件,当然还包括一些依赖npm包 const Koa = require ...
- Notification高级技巧
观察Notification这个类,你会发现里面还有很多我们没有使用过的属性.先来看看sound这个属性吧,它可以在通知发出的时候播放一段音频,这样就能够更好地告知用户有通知到来.sound 这个属性 ...
- 绿盟网站安全防护服务(vWAF)
平台: linux 类型: 虚拟机镜像 软件包: basic software devops nsfocus security waf 服务优惠价: 按服务商许可协议 云服务器费用:查看费用 立即部署 ...
- xp_delete_files不起作用解决方法
xp_delete_file用来删除数据库的备份文件和维护计划文本报告.示例: ,N'D:\Backup\Diff',N'bak',N'2019-05-29T10:03:41' 第一个参数表示文件类型 ...
- UVA Stacks of Flapjacks 栈排序
题意:给一个整数序列,输出每次反转的位置,输出0代表排序完成.给一个序列1 2 3 4 5,这5就是栈底,1是顶,底到顶的位置是从1~5,每次反转是指从左数第i个位置,将其及其左边所有的数字都反转,假 ...
- HDU3308 线段树区间合并
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 ,简单的线段树区间合并. 线段树的区间合并:一般是要求求最长连续区间,在PushUp()函数中实 ...
- IOS 导航控制器基本使用(UINavigationController代码创建方式)
● UINavigationController的使用步骤 ➢ 初始化UINavigationController ➢ 设置UIWindow的rootViewController为UINavigati ...
- 问题 B: Curriculum Vitae
问题 B: Curriculum Vitae 时间限制: 1 Sec 内存限制: 128 MB提交: 109 解决: 25[提交][状态][讨论版][命题人:acm4302] 题目描述 Hideo ...
- 2018.6.18 MyEclipse导入jquery-1.8.0.min.js等文件报错的解决方案
MyEclipse导入jQuery-1.8.0.min.js等文件的时候有时候会报了一堆missing semicolon的错误.怎么解决这个报错呢?方法如下: 1.选中报错的jquery文件例如&q ...