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 ...
随机推荐
- 0102-使用 API 网关构建微服务
一.移动客户端如何访问这些服务 1.1.客户端与微服务直接通信[很少使用] 从理论上讲,客户端可以直接向每个微服务发送请求.每个微服务都有一个公开的端点(https ://.api.company.n ...
- Springboot入门2-配置druid
Druid是Java语言中最好的数据库连接池,在连接池之外,还提供了非常优秀的监控功能. 下面来说明如何在 Spring Boot 中配置使用Druid 1.添加Maven依赖 (或jar包) < ...
- jqprint 打印网页 jQuery print plugin
ref://jQuery print plugin <!DOCTYPE html> <html lang="en"> <script src=&quo ...
- CoreThink主题开发(八)使用H-ui开发博客主题之用户登录之前及登录之后
感谢H-ui.感谢CoreThink! 效果图: 登录之后 登录窗体 想做登录之后的下拉菜单的,实在做不出来了,就一般显示了... 整个面包屑导航这里,先遍历模块,并且是允许前台显示的模块,之后就是判 ...
- python爬取百度翻译返回:{'error': 997, 'from': 'zh', 'to': 'en', 'query 问题
解决办法: 修改url为手机版的地址:http://fanyi.baidu.com/basetrans User-Agent也用手机版的 测试代码: # -*- coding: utf-8 -*- & ...
- Android:日常学习笔记(6)——探究活动(4)
Android:日常学习笔记(6)——探究活动(4) 活动的启动模式 standard模式 standard是活动默认的启动模式,在不进行显示定义的情况下,所有活动都会自动使用这种启动模式. stan ...
- js判断有无属性
访问元素属性 getAttribute 不存在返回null,特性名可不区分大小写 dom对象访问公共属性,自定义属性不能访问,div.id 访问对象属性 1.使用in关键字 该方法可以判断对象的自有属 ...
- SQL语句 自连表查询。inner join用法,partition by ,列转行查询
use mydb1 go -- 表T_Employee2 -- Id Name Position Dept -- 1 张三 员工 市场部 -- 2 李四 经理 销售部 -- 3 王五 经理 市场部 - ...
- JPEGView——专业、免费、开源的图像浏览器
虽叫JPEGView,它不仅支持jpeg图像格式,主流的图像格式它都支持. 试一试你就知道它有多强大了.
- JDK的安装配置
1.下载JDK安装包(http://www.oracle.com/technetwork/java/javase/downloads/index.html),现在Java已经更新到JDK 8了,但是很 ...