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],
] 就是排列组合的问题,使用dfs就可以解决,代码如下:
 class Solution {
public:
vector<vector<int>> combine(int n, int k) {
tmp.resize(k);
ret.clear();
dfs(, k, n, );
return ret;
} void dfs(int depth, int maxDepth, int n, int start)
{
if(depth == maxDepth){
ret.push_back(tmp);
return;
}
for(int i = start ; i <= n; ++i){
tmp[depth] = i;
dfs(depth + , maxDepth, n, i + );
}
}
private:
vector<vector<int>> ret;
vector<int> tmp;
};

java版本的如下所示,去除了所有的全局变量,看起来简洁一点:

 public class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
List<Integer> tmp = new ArrayList<Integer>();
dfs(ret, tmp, 1, n, k);
return ret;
} public void dfs(List<List<Integer>> ret, List<Integer> tmp, int start, int n, int k)
{
if(tmp.size() == k){
List<Integer> list = new ArrayList<Integer>(tmp);
ret.add(list);
return;
}
for(int i = start; i <= n; ++i){
tmp.add(i);
dfs(ret, tmp, i + 1, n, k);
tmp.remove(new Integer(i)); //这里比较重要
}
}
}

LeetCode OJ:Combinations (排列组合)的更多相关文章

  1. LeetCode 77 Combinations(排列组合)

    题目链接:https://leetcode.com/problems/combinations/#/description    Problem:给两个正数分别为n和k,求出从1,2.......n这 ...

  2. [LeetCode] 77. Combinations 全组合

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  3. 数组排列组合问题——BACKTRACKING

    BACKTRACKING backtracking(回溯法)是一类递归算法,通常用于解决某类问题:要求找出答案空间中符合某种特定要求的答案,比如eight queens puzzle(将国际象棋的八个 ...

  4. [leetcode] 题型整理之排列组合

    一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...

  5. leetCode 77.Combinations (组合)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  6. 【LeetCode每天一题】Permutations(排列组合)

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  7. LeetCode OJ:Permutations(排列)

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

  8. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  9. leetcode-Combinations 复习复习排列组合

    Combinations 题意: 根据给定的n和k,生成从1到n范围内长度为k的排列组合 示例: n=4 k=2 [[1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2 ...

随机推荐

  1. Hexo+yilia博客首页不显示全文,显示more,截断文章。

    个人主页:https://www.yuehan.online hexo new “xxx” 在md文档中 插入<!--more-->即可. 现在博客:www.wangyurui.top

  2. 安卓手机开机键失灵,FASTBOOT模式ADB重启

    安装ADB工具 CMD指令fastboot reboot

  3. C++友元概念

    采用类的机制后实现了数据的隐藏与封装,类的数据成员一般定义为私有成员,成员函数一般定义为公有的,依此提供类与外界间的通信接口. 但是,有时需要定义一些函数,这些函数不是类的一部分,但又需要频繁地访问类 ...

  4. 【leetcode刷提笔记】Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  5. 3.6《深入理解计算机系统》笔记(四)虚拟存储器,malloc,垃圾回收【插图】

    概述 ●我们电脑上运行的程序都是使用虚拟存储,跟物理内存根本不搭边. ●既然虚拟内存是在磁盘上的,为什么它又运行这么好,并没有感觉卡顿?这要感谢程序的局部性! ●虚拟存储器的调度是一个操作系统必须做好 ...

  6. Linux服务器注意事项

    1.在Linux新建一个tomcat目录,执行里面的文件运行的时候 会出现权限不足的提示?解决办法:这是因为新建的文件夹,对于可执行脚本,必须先授权,进入bin目录后,执行命令  chmod 764 ...

  7. poj 3126 Bfs

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14539   Accepted: 8196 Descr ...

  8. 基于docker环境,搭建 jetty环境, 部署java项目

    前提: 1.Ubuntu 系统. 2.docker环境已经安装好. 实现步骤: 1.上docker hub 下载jetty docker 镜像. 执行命令:$ sudo docker pull jet ...

  9. Sqoop将MySQL表结构同步到hive(text、orc)

    Sqoop将MySQL表结构同步到hive sqoop create-hive-table --connect jdbc:mysql://localhost:3306/sqooptest --user ...

  10. MapReduce-join连接

    join连接 MapReduce能够执行大型数据集间的连接(join)操作.连接操作的具体实现技术取决于数据集的规模及分区方式连接操作如果由mapper执行,则称为“map端连接”:如果由reduce ...