原题地址:https://oj.leetcode.com/problems/combinations/

题意:组合求解问题。

解题思路:这种求组合的问题,需要使用dfs来解决。

代码:

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

 

相关文章:

  • 2021-05-23
  • 2022-12-23
  • 2021-08-17
  • 2021-07-22
  • 2022-12-23
  • 2022-12-23
  • 2021-04-25
  • 2021-08-05
猜你喜欢
  • 2021-06-12
  • 2021-08-15
  • 2021-08-02
  • 2022-01-10
相关资源
相似解决方案