[LeetCode][Java] 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],
]
题意:
给定两个整数 n 和 k,返回1 ... n中k个数字的全部的组合。
例如说。
假设n=4 and k=2,解为:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
算法分析:
用一个循环递归处理子问题。
AC代码:
public class Solution
{
public ArrayList<ArrayList<Integer>> combine(int n, int k)
{
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if(n<=0 || n<k)
return res;
helper(n,k,1,new ArrayList<Integer>(), res);
return res;
}
private void helper(int n, int k, int start, ArrayList<Integer> item, ArrayList<ArrayList<Integer>> res)
{
if(item.size()==k)
{
res.add(new ArrayList<Integer>(item));
return;
}
for(int i=start;i<=n;i++) // try each possibility number in current position
{
item.add(i);
helper(n,k,i+1,item,res); // after selecting number for current position, process next position
item.remove(item.size()-1); // clear the current position to try next possible number
}
}
}
[LeetCode][Java] Combinations的更多相关文章
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- Java for LeetCode 077 Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- Letter Combinations of a Phone Number leetcode java
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- Combinations leetcode java
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...
- [LeetCode][Java] Letter Combinations of a Phone Number
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- LeetCode Factor Combinations
原题链接在这里:https://leetcode.com/problems/factor-combinations/ 题目: Numbers can be regarded as product of ...
- LeetCode 77 Combinations(排列组合)
题目链接:https://leetcode.com/problems/combinations/#/description Problem:给两个正数分别为n和k,求出从1,2.......n这 ...
- [LeetCode] Factor Combinations 因子组合
Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...
- [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
随机推荐
- [python 函数学习篇] 关键字参数
函数可以通过 关键字参数 的形式来调用,形如 keyword = value .例如,以下的函数: def parrot(voltage, state='a stiff', action='voom' ...
- 视频播放插件Video.js
这是一个很强大的视频播放插件.
- C/C++、Java、Python谁是编译型语言,谁是解释型语言?
最近各大互联网公司线上笔试,编程题目里的编译器只支持C/C++.Java,甚至有的支持javaScrpit和Pascal,就是不支持Python.让一直以来用惯了Python的我直吐血,于是今天痛定思 ...
- 【Luogu】P4208最小生成树计数(状压乱搞)
题目链接 最小生成树有两个性质,两个性质都知道的话这题就变成码农题了. 1.无论最小生成树长什么样,所有权值的边的数量是不变的.比如我有棵最小生成树有两条权值为2的边四条权值为1的边,那这个图的所有最 ...
- ubuntu安装jdk<服务器>
服务器 阿里云服务器Ubuntu安装jdk7 2014-08-25 16:44 | coding云 | 5825次阅读 | 6条评论 一.下载jdk 可以先下载到本地,然后ftp到服务器 官方 ...
- formData使用总结
1.formData基本使用 //可以从form元素初始化一个FormData对象,或者new一个空对象 var formData = new FormData([fromElement]); //可 ...
- 刷题总结——String painter(hdu2476)
题目: Problem Description There are two strings A and B with equal length. Both strings are made up of ...
- 随机生成指定长度字符字符串(C语言实现)
相关函数 srand(), rand()头文件#include<stdlib.h> 定义函数 int rand(void) 函数说明 rand()会返回一随机数值,范围在0至RAND_MA ...
- unity3d各平台通讯原生的平台API的说明
注意:unity3d与原生代码的调用需要pro版本,此点注意了. 一.IOS平台,由于IOS平台的原生应该是objectC,所以通讯起来非常的简单, 1.原生代码调用u3d代码: 1.1.在Xcode ...
- SQLite的sqlite_master表
SQLite的sqlite_master表 sqlite_master表是SQLite的系统表.该表记录该数据库中保存的表.索引.视图.和触发器信息.每一行记录一个项目.在创建一个SQLIte数据 ...