LeetCode OJ: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],
] 就是排列组合的问题,使用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 (排列组合)的更多相关文章
- LeetCode 77 Combinations(排列组合)
题目链接:https://leetcode.com/problems/combinations/#/description Problem:给两个正数分别为n和k,求出从1,2.......n这 ...
- [LeetCode] 77. Combinations 全组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 数组排列组合问题——BACKTRACKING
BACKTRACKING backtracking(回溯法)是一类递归算法,通常用于解决某类问题:要求找出答案空间中符合某种特定要求的答案,比如eight queens puzzle(将国际象棋的八个 ...
- [leetcode] 题型整理之排列组合
一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...
- leetCode 77.Combinations (组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 【LeetCode每天一题】Permutations(排列组合)
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- LeetCode OJ:Permutations(排列)
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- leetcode-Combinations 复习复习排列组合
Combinations 题意: 根据给定的n和k,生成从1到n范围内长度为k的排列组合 示例: n=4 k=2 [[1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2 ...
随机推荐
- graoovy资料
官网 http://www.groovy-lang.org/ 官方文档 http://www.groovy-lang.org/documentation.html Groovy入门教程 http:// ...
- RHEVM 相关介绍
基础概念: RHEV-H RHEVH(Redhat Enterprise Virtuallization Hypervisor),它是运行虚拟机所需的最低操作系统.RHEVH由作为RHEL(Redha ...
- 剑指offer 面试58题
面试58题: 题目:翻转字符串 题:牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意 ...
- hadoop本地运行与集群运行
开发环境: windows10+伪分布式(虚拟机组成的集群)+IDEA(不需要装插件) 介绍: 本地开发,本地debug,不需要启动集群,不需要在集群启动hdfs yarn 需要准备什么: 1/配置w ...
- PHP......会话控制SESSION与COOKIE
一.SESSION Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 Web 页之间跳转时,存储在 ...
- 17南宁区域赛 I - Rake It In 【DFS】
题目链接 https://nanti.jisuanke.com/t/19975 题意 Alice 和 Bob 玩游戏 在一个4x4 的方格上 每个人 每次选择2x2的区域 将里面的四个值求和加到最后的 ...
- iOS 单例模式 学习 "52个方法 第6章 45条 使用 dispath_once 来执行只需运行一次的线程安全代码"
百度定义:单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中一个类只有一个实例. 维基百科:在软件工程中,单例是一种用于实现单例的数学概念,即将 ...
- SpringBoot Redis工具类封装
1.接口类 package com.sze.redis.util; import java.util.List; import java.util.Set; import java.util.conc ...
- curl操作封装
<?php /** * Class Curl curl简单封装 get post */ class Curl { /** * @brief get请求 * @param $url 请求的url ...
- 【HackerRank】Median
题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大 ...