[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 ...
随机推荐
- HTTP的一些概念
1. 什么是回调? 回调是异步编程时的基础,将后续逻辑封装成起始函数的参数,逐层嵌套 2. 什么是同步/异步? 同步是指:发送方发出数据后,等接收方发回响应以后才发下一个数据包的通讯方式. 异步是指: ...
- 缓存淘汰算法之LRU实现
Java中最简单的LRU算法实现,就是利用 LinkedHashMap,覆写其中的removeEldestEntry(Map.Entry)方法即可 如果你去看LinkedHashMap的源码可知,LR ...
- DS博客作业06——图
1.本周学习总结(0--2分) 1.思维导图 2.谈谈你对图结构的认识及学习体会. 这章学习了图,学习了图的两种存储结构:邻接矩阵和邻接表.这两种存储结构都用到了之前学c时学到的结构体,将结构体充分运 ...
- RabbitMQ与PHP(一)
RabbitMQ与PHP(一) 项目中使用RabbitMQ作为队列处理用户消息通知,消息由前端PHP代码产生,处理消息使用Python,这就导致代码一致性问题,调整消息定义时需要PHP和Python都 ...
- Welcome-to-Swift-02基本运算符
运算符是检查,改变,合并值的特殊符号或短语.例如,加号+将两个数相加(如let i = 1 + 2).复杂些的运行算例如逻辑与运算符&&(如if enteredDoorCode &am ...
- 【Luogu】P3343地震后的幻想乡(对积分概率进行DP)
题目链接 神难qwq.配合rqy的博客食用. 首先我们学到有一个概率函数$p(x)$表示某事件发生概率取值小于x的函数.这个函数有什么特点呢? 那就是$\int_{-∞}^{∞}p(x)dx=1$ 这 ...
- win 7 取得最高权限
以管理员身份运行cmd,然后输入: net user administrator /active:yes 然后注销,就会看到你原来的用户已经是最高权限的用户了.以后做的操作都是最高权限的操作.
- (转)Java字符串整形(例:0001)
原文地址:https://blog.csdn.net/xuexiiphone/article/details/51372692 caseID = preFix + String.format(&quo ...
- postgresql 10 ltree 使用说明
官方文档 https://www.postgresql.org/docs/10/static/ltree.html ltree是俄罗斯Teodor Sigaev和Oleg Bartunov共同开发的P ...
- Drupal service module 介绍
https://www.ostraining.com/blog/drupal/services/ https://www.drupal.org/node/1246470 https://www.dru ...