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 ...
随机推荐
- 瑞丽熵(renyi entropy)
在信息论中,Rényi熵是Hartley熵,Shannon熵,碰撞熵和最小熵的推广.熵能量化了系统的多样性,不确定性或随机性.Rényi熵以AlfrédRényi命名.在分形维数估计的背景下,Rény ...
- 20170330 ABAP代理生成
在线文档ABAP代理生成, ABAP 代理生成(事物SPROXY)使您能够通过使用企业服务资源中企业服务资源库中 的接口描述在SAP系统中生成ABAP代理对象. ABAP代理生成概览: 1.代理标识, ...
- 百度地图sn计算方法说明
官方说明[有状态码的数值意义查询 和 python 转换源码]: http://lbsyun.baidu.com/index.php?title=lbscloud/api/appendix
- key points & (QA) about RPKI
@1: Q: What does ROA look like?Since ROA means which ASes are allowed for originating routes to some ...
- Efficiency in Shell
最近在写一个shell脚本, 由于该脚本对效率有较高的要求, 所以查阅了一些文章. 感觉这篇文章写得确实不错, 文章中的例子,确实很棒! 所 以我把他们提取出来: @1:实例: 需求:计算1到1000 ...
- MySQL数据库(7)_MySQL 数据备份与还原
一.数据备份 1.使用mysqldump命令备份 mysqldump命令将数据库中的数据备份成一个文本文件.表的结构和表中的数据将存储在生成的文本文件中. mysqldump命令的工作原理很简单.它先 ...
- NetCDF 介绍
NetCDF 1 NetCDF 1.1概述(Overview) NetCDF (network Common Data Form) is a set of software libraries and ...
- JSP笔记05——生命周期(转)
原始内容:https://www.tutorialspoint.com/jsp/jsp_life_cycle.htm 在这一章中,我们将讨论JSP的生命周期. 理解JSP低层次功能的关键在于——理解它 ...
- jQuery消息提示框插件Tipso
在线演示 本地下载
- 我的python开发目录模块连接
一.python语言 二.HTML 三.css 四.javascript 五.DOM 六.jquery 七.AJAX 八.WEB前端插件 九.自定义WEB框架 十.WEB框架之tornado 十一.M ...