077 Combinations 组合
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。
例如,
如果 n = 4 和 k = 2,组合如下:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
详见:https://leetcode.com/problems/combinations/description/
Java实现:
class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
List<Integer> out=new ArrayList<Integer>();
helper(n,k,1,out,res);
return res;
}
private void helper(int n,int k,int start,List<Integer> out,List<List<Integer>> res){
if(out.size()==k){
res.add(new ArrayList<Integer>(out));
}
for(int i=start;i<=n;++i){
out.add(i);
helper(n,k,i+1,out,res);
out.remove(out.size()-1);
}
}
}
参考:http://www.cnblogs.com/grandyang/p/4332522.html
077 Combinations 组合的更多相关文章
- [FollowUp] Combinations 组合项
这是Combinations 组合项 的延伸,在这里,我们允许不同的顺序出现,那么新的题目要求如下: Given two integers n and k, return all possible c ...
- [LeetCode] Combinations 组合项
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 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. Example: I ...
- leetCode 77.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 ...
- Java for LeetCode 077 Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [leetcode]77. Combinations组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
- Leetcode77. Combinations组合
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], [1,3] ...
随机推荐
- poj3208启示录——数位DP
题目:http://poj.org/problem?id=3208 数位DP,首先按位数预处理出每一种位数的情况,包括有多少个魔鬼数和有多少个以6开头的非魔鬼数,以便递推.累加等等: 然后先找出第X个 ...
- MySQL安装过程中对The error code is 2203的解决方案
MySQL安装过程中对The error code is 2203的解决方案 1.问题描述 Windows系统安装MySQL遇到The error code is 2203.,具体描述如下 The i ...
- Scala学习——array与arraybuffer的区别(初)
1.由于Array是不可变的,所以不能直接地对其元素进行删除操作,只能通过重赋值或过滤生成新的Array的方式来删除不要的元素. 而ArrayBuffer是可变的,本身提供了很多元素的操作,当然包括删 ...
- Grunt:GruntFile.js
ylbtech-Grunt:GruntFile.js 1.返回顶部 1. module.exports = function (grunt) { grunt.initConfig({ useminPr ...
- .NETFramework:ConfigurationManager
ylbtech-.NETFramework:ConfigurationManager 1.程序集 System.Configuration, Version=4.0.0.0, Culture=neut ...
- 使用外网控制你的STM32单片机
转自:http://blog.csdn.net/xdxlove/article/details/50837459 本文章假设读者已经在STM32单片机上成功移植LWIP,且已经实现在局域网内控制STM ...
- JVM中的Hello World运行机制:
栗子: package zagoo; public class HelloWorld { public static String HELLOWORLD="Hello World" ...
- Spring Web MVC 的HandlerMapping的使用之-------SimpleUrlHandlerMapping
转自:https://www.2cto.com/kf/201401/271141.html Spring MVC教程 映射处理器Handler Mapping 一.简析映射处理器 在spring mv ...
- 如何在模板类中使用这些point类型?
博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=271 由于PCL模块较多,并且是一个模板库,在一个源文件里包含很多PCL算法 ...
- Spring入门第十八课
Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或者基于XML配置的AOP 看代码: package lo ...