[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],

[1,3],

[1,4]]

Notice

You don‘t need to care the order of combinations, but you should make sure the numbers in a combination are sorted.

我的代码

class Solution:
    """
    @param n: Given the range of numbers
    @param k: Given the numbers of combinations
    @return: All the combinations of k numbers out of 1..n
    """
    def combine(self, n, k):
        # write your code here
        res = []
        s1 = [[]]
        s2 = []
        for i in range(1,n+1):
            while(s1):
                tmp1 = s1.pop()
                tmp2 = tmp1+[i]
                if len(tmp2) == k:
                    res.append(tmp2)
                else:
                    s2.append(tmp2)
                if k-len(tmp1)<=n-i:
                    s2.append(tmp1)
            s1 = s2
            s2 = []
        return res

别人的代码

https://leetcode.com/problems/combinations/discuss/27024/1-liner-3-liner-4-liner

1. Library

from itertools import combinations

class Solution:
    def combine(self, n, k):
        return list(combinations(range(1, n+1), k))

2.Recursive - AC in 76 ms

有点像动态规划

class Solution:
    def combine(self, n, k):
        if k == 0:
            return [[]]
        return [pre + [i] for i in range(k, n+1) for pre in self.combine(i-1, k-1)]

3.Iterative - AC in 76 ms

class Solution:
    def combine(self, n, k):
        combs = [[]]
        for _ in range(k):
            combs = [[i] + c for c in combs for i in range(1, c[0] if c else n+1)]
        return combs

4. Reduce - AC in 76 ms

```python

class Solution:

def combine(self, n, k):

return reduce(lambda C, _: [[i]+c for c in C for i in range(1, c[0] if c else n+1)],

range(k), [[]])```

思路

我当时的想法是,对每个元素,有可能存在或不存在于某个结果中。其次,如果最后余下对数均加入一个结果中仍不足k,就提前舍弃。

原文地址:https://www.cnblogs.com/siriusli/p/10381462.html

时间: 2024-10-06 20:46:16

[Lintcode]152. Combinations/[Leetcode]77. Combinations的相关文章

leetcode 77 Combinations ----- 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], ] 求C(k,n)的所有结果. 利用回溯(backtracking)来求解释道题是比较简单且效率较高的. public class Sol

leetCode 77.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个数的组合.没有反复(位置交换算反复).可用排列组合的统一公式求解. 代码例如以下: p

leetcode[77] Combinations

给定n和k,从1到n中选k个数,存到结果中.其实就是组合问题.例如 If n = 3, k = 2, 结果是 { 1,2], [1,3], [2,3] }; 思路:利用回溯法. class Solution { public: void dfs77(vector<vector<int > > &ans, vector<int> subans, int start, int n, int k) { if (subans.size() == k) { ans.pus

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"

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为每次尝试的不同

Leetcode:Letter Combinations of a Phone Number 手机键盘字母映射

Letter Combinations of a Phone Number: 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" Outp

LeetCode: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], ]DFS,递归 1 class Solution { 2 public: 3 vector<vector<int> > an

【一天一道LeetCode】#77. Combinations

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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]

&lt;LeetCode OJ&gt; 77. Combinations

Total Accepted: 69360 Total Submissions: 206274 Difficulty: Medium 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], ]