题目:

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的更多相关文章

  1. 【Permutations】cpp

    题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...

  2. 【Subsets】cpp

    题目: Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset ...

  3. 【Anagrams】 cpp

    题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...

  4. 蓝桥杯 【dp?】.cpp

    题意: 给出一个2*n的方格,当刷完某一个方格的漆后可以且只可以走到相邻的任何一格,即上 下 左 右 左上 左下 右上 右下.可以从任意一个格子开始刷墙,问有多少种刷法,因为随着n的增大方案数会变多, ...

  5. 【Triangle 】cpp

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  6. 【N-Queens】cpp

    题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...

  7. 【Candy】cpp

    题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...

  8. 【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 = ...

  9. 【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 ...

随机推荐

  1. Apache Solr-6.0.1 (OpenLogic CentOS 7.2)

    Apache Solr-6.0.1 (OpenLogic CentOS 7.2) 平台: CentOS 类型: 虚拟机镜像 软件包: java1.8 solr6.0.1 application ser ...

  2. Hyper-V 2016 配置管理系列(部署篇)

    Hyper主机前提准备以后,我们开始Hyper-V Cluster 群集配置 准备验证Cluster 群集 : 1)打开群集管理器,点击"validate Configuration&quo ...

  3. SQLServer从其他表获取的数据更新该表的一部分

    在网上常见的是update  a  set  username  =  username  FROM b  on a.userid=b.userid,该更新语句是对a表中所有行进行更新.如果只更新一部 ...

  4. php使用GD库实现图片水印和缩略图——给图片添加文字水印

    今天呢,就来学习一下在php中使用PD库来实现对图片水印的文字水印方法,不需要PS哦! 首先,准备素材 (1)准备一张图片 (2)准备一张水印(最好是透明的,即背景是白色底) (3)准备一中字体(在电 ...

  5. VMware Workstation Pro 11、12 密钥

    11:1F04Z-6D111-7Z029-AV0Q4-3AEH8 12:5A02H-AU243-TZJ49-GTC7K-3C61N

  6. MySQL常用命令总结1

    默认已成功安装并配置MySQL,以下命令全部在CMD命令行窗口(Win10平台)中进行输入: mysql -uusername -ppassword //登录MySQL MYSQL -V //查看My ...

  7. webpack最简单的入门教程里bundle.js之运行单步调试的原理解析

    读这篇文章的朋友,请确保对webpack有最基础的认识. 您可以阅读我前一篇文章:Webpack 10分钟入门 来在本地运行一个Webpack的hello world项目.https://www.to ...

  8. mongdb 一些操作

    一.命令操作数据库1.管理员身份打开cmd2.进到mongdb的mongo.exe文件所在路径3.show dbs 查看mongodb4.连接远程数据库:mongo ip:端口/数据库5.打开某个数据 ...

  9. preprocessing MinMaxScaler

    import numpy as npfrom sklearn.preprocessing import MinMaxScalerdataset = np.array([1,2,3,5]).astype ...

  10. axios获取后端数据

    axios向后端请求数据时,一直获取不到数据, 后来改成这样写获取到了数据 不是一个this,有人说用箭头函数就可以了.