[LeetCode]Combinations@python

本文介绍了解决组合问题的三种方法:使用深度优先搜索(DFS),递归思想以及栈的应用。通过具体的示例,如从1到n中选择k个数的所有可能组合,详细解释了每种方法的实现过程及原理。

Given two integers n and k, return all possible combinations ofk 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,但是会报TLE

class Solution:
    # @return a list of lists of integers
    def combine(self, n, k):
        def dfs(start, valuelist):
            if self.count == k: res.append(valuelist); return
            for i in range(start, n + 1):
                self.count += 1
                dfs(i + 1, valuelist + [i])
                self.count -= 1
        res = []; self.count = 0
        dfs(1, [])
        return res

思路二:用递归思想。在n个数中选k个,如果n大于k,那么可以分类讨论,如果选了n,那么就是在1到(n-1)中选(k-1)个,否则就是在1到(n-1)中选k个。递归终止的条件是k为1,这时候1到n都符合要求。

class Solution:
    def combine(self, n, k):
        if k == 1:
            return [[i + 1] for i in range(n)]
        res = []
        if n > k:
            res = [r + [n] for r in self.combine(n-1, k-1)] + self.combine(n-1, k)
        else:
            res = [r + [n] for r in self.combine(n-1, k-1)]
        return res    
解法三:用栈。这里要注意n-x+1 < k-1的意思是:

One combination has k numbers, and currently we already have l numbers, so we need to find another k-l numbers. Since we insert the numbers into stack in the ascending order, we want to make sure that there are enough larger numbers to be inserted. Fromx to n, there are n-x+1 numbers that are larger than the numbers in the current stack. So ifn-x+1 < k-l, it means that not enough larger numbers to be inserted, so we track back.


class Solution:
    def combine(self, n, k):
        ans = []
        stack = []
        x = 1
        while True:
            l = len(stack)
            if l == k:
                ans.append(stack[:])
            if l == k or n-x+1 < k-l:
                if not stack:
                    return ans
                x = stack.pop() + 1
            else:
                stack.append(x)
                x += 1


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值