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));
            }
        }
    }
时间: 2024-07-30 13:35:25

Combinations ——LeetCode的相关文章

Combinations leetcode java

题目: 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], ] 题解: 这道题就是用DFS(参考Work BreakII)的循环递归处理子问题的方法解决.n为循环的次数,k为每次尝试的不同

[Lintcode]152. Combinations/[Leetcode]77. Combinations

152. Combinations/77. Combinations 本题难度: Medium Topic: Search & Recursion Description Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example Given n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2]

Combinations &lt;leetcode&gt;

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], ] 算法:该题很简单,但是当时没想到,上网上一查才恍然大悟,就是构造一个解空间,然后用回溯解决,soeasy,不说了,上代码: 1 cla

Solution to LeetCode Problem Set

Here is my collection of solutions to leetcode problems. LeetCode - Course Schedule LeetCode - Reverse Linked List LeetCode - Isomorphic Strings LeetCode - Count Primes LeetCode - Remove Linked List Elements LeetCode - Happy Number LeetCode - Bitwise

LeetCode: Letter Combinations of a Phone Number [018]

[题目] Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae"

[LeetCode][Python]17: Letter Combinations of a Phone Number

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 17: Letter Combinations of a Phone Numberhttps://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/ Given a digit string, return all possible letter combinations that the number c

LeetCode: Combinations [077]

[题目] Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". Note: If there is no such

[LeetCode] Combinations [38]

题目 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], ] 原题链接(点我) 解题思路 组合问题,老思路---递归加循环,这个是组合里面比较简单的. 代码实现 class Solutio

Letter Combinations of a Phone Number -- leetcode

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q