Combinations ——LeetCode
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,输出所有长度为k的排列组合。
解题思路:当做求子集来做,大于k的剪枝剪掉,最后再筛一下结果集。
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res = new ArrayList<>();
if(n==0){
return res;
}
res.add(new ArrayList<Integer>());
helper(res,k,n);
Iterator<List<Integer>> itr = res.iterator();
while(itr.hasNext()){
if(itr.next().size()!=k){
itr.remove();
}
}
return res;
}
private void helper(List<List<Integer>> res,int k,int n){
for(int j=1;j<=n;j++){
int currSize = res.size();
for(int i=0;i<currSize;i++){
List<Integer> x = new ArrayList<>(res.get(i));
x.add(j);
if(x.size()>k)
continue;
res.add(new ArrayList<>(x));
}
}
}
Combinations ——LeetCode的更多相关文章
- Combinations [LeetCode]
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- Combinations leetcode java
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- [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] Combinations 组合项
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- LeetCode Factor Combinations
原题链接在这里:https://leetcode.com/problems/factor-combinations/ 题目: Numbers can be regarded as product of ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
随机推荐
- (转)VS2012网站发布详细步骤
2.弹出网站发布设置面板,点击<新建..>,创建新的发布配置文件: 4. 在配置中,要选择“Release”——发布模式(Release 称为发布版本,它往往是进行了各种优化,使得程序 ...
- 关于一些Android冷知识
1. 在Android4.0以后,EditText就由以前的输入框变成了一条划线的输入方式,如需要变为老版本的,只需在layout里面引入代码: android:background="@a ...
- 测测你适合从事Web前端开发吗
一般初创的互联网公司最烧钱的时候往往都是刚刚获得风投或融资的时候,因为他们要把钱砸向前端,因为那时候没有客户访问,对于企业来说只有先做好前端技 术.做好客户体验一切才有可能.用户体验做好,才有人访问, ...
- iis6 下发布MVC2项目的方法
1.安装MVC2运行库,否则会出现错误 [以下转载]http://blog.csdn.net/xw13106209/article/details/6323695 错误:”未能加载文件或程序集“Sys ...
- 分享最近和同事处理的 解析XML的相关问题
CREATE OR REPLACE PROCEDURE BATCHINSERTSK_DEVICE_RECORD1( xmlstr IN clob, v_commits o ...
- mysql基础操作整理(一)
显示当前数据库 mysql> select database(); +------------+ | database() | +------------+ | test | +-------- ...
- iOS 数据持久性存储-对象归档
对象归档是将对象归档以文件的形式保存到磁盘中(也称为序列化,持久化),使用的时候读取该文件的保存路径读取文件的内容(也称为解档,反序列化) 主要涉及两个类:NSKeyedArichiver.NSKey ...
- html中发送邮箱的链接
- VB.NET中LINQ TO List泛型查询语句(分组,聚合函数)
Public Class LinqToList 'LINQ在C#中使用比较方便,但是在VB中使用比较麻烦,复杂,和C#用法并不太一样 Dim listNew As List(Of Product) = ...
- ECSHOP在商品详细页面上获取该商品的顶级分类id和名称
在 goods.php 文件, 找到 $smarty->assign('goods', $goods); 在它上面增加下面代码: 方法一: $cat_arr = get_parent_cats( ...