[leetcode]Combinations @ Python

原题地址: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
原文地址:https://www.cnblogs.com/zuoyuan/p/3757165.html