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的更多相关文章

  1. Combinations [LeetCode]

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  2. Combinations leetcode java

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...

  3. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  4. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  5. [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 ...

  6. [LeetCode] Combinations 组合项

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  7. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  8. LeetCode Factor Combinations

    原题链接在这里:https://leetcode.com/problems/factor-combinations/ 题目: Numbers can be regarded as product of ...

  9. [leetcode 17]Letter Combinations of a Phone Number

    1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...

随机推荐

  1. Redis Admin UI

    https://github.com/ServiceStackApps/RedisAdminUI 最近的v4版本不能用,需要下载v3版本,下载地址 https://github.com/Service ...

  2. Nhibernate主子表查询

    假如有AB两表,A为主,B为子:已知A表ID,查B表数据 ICriteria criteriaTotal = session.CreateCriteria<B>().CreateCrite ...

  3. oracle官方文档- length篇

    一.首先介绍下单字节字符集和 多字节字符集 2.2字符编码方案 2.2.1 单字节编码     (1)单字节7位字符集,可以定义128个字符,最常用的字符集为 US7ASCII     (2)单字节8 ...

  4. IOS UIColor 自定义颜色

    使用 UIColor定义颜色 和 同 CIColor 与  CGColor 之间的联系.转换 1. 利用UIColor展现 #F6F6F6 这个传统的颜色 #F6F6F6 为一个 16 进制表示的RP ...

  5. [转]PHP取整函数:ceil,floor,round,intval的区别详细解析

    我们经常用到的PHP取整函数,主要是:ceil,floor,round,intval. 1.ceil -- 进一法取整 说明float ceil ( float value ) 返回不小于 value ...

  6. 【工具篇】xshell

    SSH.telnet.串口登录等,类似Secure CRT,蛮好用的. 中文显示乱码的解决方法,file->properties,在Encoding那里修改为UTF-8 修改颜色,点Edit修改 ...

  7. 调用数据库过程函数mysql

    Connection conn=JdbcUtil.getConnection();//JdbcUtil是我写的获取connection的工具类 CallableStatement cast=conn. ...

  8. topcoder算法练习2

    Problem Statement      In most states, gamblers can choose from a wide variety of different lottery ...

  9. 计算机天才Aaron Swartz 名作 《如何提高效率》——纪念真正的“hacker"!

    如何提高效率 <HOWTO: Be more productive>(如何提高效率)作者:Aaron Swartz 肯定有人跟你说过这样的话,“你有看电视的那么长时间,都可以用来写一本书了 ...

  10. 原生js实现回到顶部

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...