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. 转载:修改xshell中文乱码的问题(管用)

    执行echo $LANG命令输出的是当前的编码方式,执行locale命令得到系统中所有可用的编码方式.要让Xshell不显示乱码,则要将编码方式改为UTF-8. 在Xshell中[file]-> ...

  2. c++ Cout 输出格式

    控制符是在头文件iomanip.h中定义的对象.使用前必须把iomanip.h包含进来 1. I/O的书写格式 I/0流是输入或输出的一系列字节,当程序需要在屏幕上显示输出时,可以使用插入操作符“&l ...

  3. Tensor神经网络进行知识库推理

    本文是我关于论文<Reasoning With Neural Tensor Networks for Knowledge Base Completion>的学习笔记. 一.算法简介 网络的 ...

  4. ajax 操作全局监测,用户session失效

    jQuery(function ($) { // 备份jquery的ajax方法 var _ajax = $.ajax; // 重写ajax方法,先判断登录在执行success函数 $.ajax = ...

  5. Android JIN返回结构体

    一.对应类型符号 Java 类型     符号 boolean     Z byte     B char     C short     S int     I long     J float   ...

  6. SGU 197.Nice Patterns Strike Back

    时间限制:0.5s 空间限制:6M 题意: 给出长n(n<=10^100)和宽m(m<=5)的地面,铺上黑色和白色的地板,使得没有任意一个2*2大小的地面铺同种颜色的方案数是多少. Sol ...

  7. Linq查询IEnumerable与IQueryable

    class Program { static void Main(string[] args) { System.Diagnostics.Stopwatch stp = new Stopwatch() ...

  8. 【JQuery学习历程】2.JQuery选择器

    基本选择器 选择器 描述 返回 示例 #id 根据给定的id匹配元素 单个元素 $("#myId") .class 根据给定的class类匹配元素 集合元素 $(".my ...

  9. Bootstrap_Javascript_手风琴

    触发手风琴可以通过自定义的 data-toggle 属性来触发.其中data-toggle值设置为 collapse,data-target="#折叠区标识符". 第一步,设计一个 ...

  10. php基础知识【函数】(5)正则preg

    一.匹配次数 (1) * 匹配前面的子表达式零次或多次 (2) + 匹配前面的子表达式一次或多次,+ 等价于 {1,} (3) ? 匹配前面的子表达式零次或一次,? 等价于 {0,1} (4){n} ...