[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 ...
随机推荐
- 项目报错 java lang illegalargumentexception error at 0 can t find referenced pointcut
出现error at ::0 can't find referenced pointcut...这样的错误原因是:如果你用的JDK版本是1.6的话,而引用的aspectjrt.jar是spring-2 ...
- 爬虫Scrapy框架-1
Scrapy 第一步:安装 linux: pip3 install scrapy windows: 1:pip3 install wheel ,安装wheel模块 2.下载twisted:http:/ ...
- spring AOP详解〇
AOP正在成为软件开发的下一个圣杯.使用AOP,你可以将处理aspect的代码注入主程序,通常主程序的主要目的并不在于处理这些aspect.AOP可以防止代码混乱. 为了理解AOP如何做到这点,考虑一 ...
- API经济时代的思考(转载目的:为之后写API-first模式的生命周期治理做准备)
原文地址:API经济时代的思考 感觉这篇博客还不错,个人赞同其大部分的内容,借鉴参考一下,懒得自己写了(关键是不一定能轻松写得更好,嘿嘿,偷懒啦) 接下来会写关于API经济的概念下,如何进行AP ...
- BZOJ3160 万径人踪灭 【fft + manacher】
题解 此题略神QAQ orz po神牛 由题我们知道我们要求出: 回文子序列数 - 连续回文子串数 我们记为ans1和ans2 ans2可以用马拉车轻松解出,这里就不赘述了 问题是ans1 我们设\( ...
- ApplicationContext,WebApplicationContext
servletContext 是web应用程序的大环境,用于存储整个web应用程序级别的对象. ApplicationContext,WebApplicationContext 是Spring的Bea ...
- vue2 vuex 简单入门基础
1.vuex中文文档 https://vuex.vuejs.org/zh-cn/api.html 2.我理解vuex 应用观察者模式 设置了全局的状态 state 状态变化即通知更新全局state 3 ...
- es6总结(八)--模块化-import
- PXC部署,配置,操作原理
参考:https://www.cnblogs.com/kevingrace/p/5685371.html?utm_source=itdadao&utm_medium=referra ...
- 转 Python爬虫入门三之Urllib库的基本使用
静觅 » Python爬虫入门三之Urllib库的基本使用 1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由浏览器 ...