LeetCode——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],
]
原题链接:https://oj.leetcode.com/problems/combinations/
题目:给定两个整数n和k,返回全部的k个1……n中的数的组合。
思路:递归。
public List<List<Integer>> combine(int n, int k) {
return combine(1, n + 1, k);
} public List<List<Integer>> combine(int low, int upper, int k) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (k == 1) {
for (int i = low; i < upper; i++) {
List<Integer> r = new ArrayList<Integer>();
r.add(i);
result.add(r);
}
return result;
}
for (int i = low; i < upper; i++) {
List<List<Integer>> r = combine(i + 1, upper, k - 1);
for (List<Integer> a : r) {
a.add(0, i);
}
result.addAll(r);
}
return result;
}
reference : http://www.laurashawn.net/?p=10907
LeetCode——Combinations的更多相关文章
- [LeetCode] Combinations 组合项
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- leetcode — combinations
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...
- [leetcode]Combinations @ Python
原题地址:https://oj.leetcode.com/problems/combinations/ 题意:组合求解问题. 解题思路:这种求组合的问题,需要使用dfs来解决. 代码: class S ...
- [LeetCode] Combinations (bfs bad、dfs 递归 accept)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [LeetCode] Combinations [38]
称号 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exa ...
- LeetCode: Combinations 解题报告
Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... ...
- [Leetcode] combinations 组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [LeetCode] Combinations 回溯
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [LeetCode] Combinations——递归
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
随机推荐
- github版本库使用详细图文教程(命令行及图形界面版)
投稿:mdxy-dxy 字体:[增加 减小] 类型:转载 时间:2015-08-06我要评论 今天我们就来学习github的使用,我们将用它来管理我们的代码,你会发现它的好处的,当然是要在本系列教程全 ...
- IISExpress实现外部访问
首先修改IISExpress配置文件 \IISExpress\config\applicationhost.config 在website中添加一个binding <binding protoc ...
- Anndroid 开发架构读书笔记
市面上大部分应用不外乎就是颠过来倒过去地做以下这些事情: --------------- --------------- --------------- --------------- | | | | ...
- npm总是安装不成功,而且很慢?
什么方法解决: 在cmd 里面先运行 npm config set registry "http://registry.npm.taobao.org" 然后再安装npm 就会很快 ...
- bzoj 2141 : 排队 分块
题目链接 2141: 排队 Time Limit: 4 Sec Memory Limit: 259 MBSubmit: 1169 Solved: 465[Submit][Status][Discu ...
- String的构造函数、析构函数和赋值函数
编写类String的构造函数.析构函数和赋值函数 已知类String的原型为: class String { public: String(const char *str = NULL); // 普通 ...
- RHEL4-Partition Image系统备份(软件版)
对于BBS,或Apache,PHP等相关网页的程序 备份: 1)/var/www/html目录,里面有PHP所写成的网页.此网页主要功能是从资料库中读取由信件存入的文章,或是使用者选择由网页输入资料时 ...
- checkbox 全选反选实现全代码
//跳转到指定action function validateForm(url){ if($("#form").form('validate')){ var x=document. ...
- mongoose查询特定时间段文档的方法
db.collection.find({ time:{ "$gte": new Date('2014-01-24'), "$lte":new Date('201 ...
- tocken和ticket的数据模型;
/* jshint -W079 */ /* jshint -W020 */ "use strict"; var _ = require("lodash"); m ...