Leetcode 77 Posted on 2019-04-15 | | 阅读数 Words count in article: 79 | Reading time ≈ 1 Question Solution使用DFS,修改46题的代码。更改dfs嵌套的退出语句。 12345678910111213class Solution: def combine(self, n: int, k: int) -> List[List[int]]: results = [] nums = list(range(1, n + 1)) self.dfs(k, nums, [], results) return results def dfs(self, k, nums, temp, results): if len(temp) == k: results.append(temp) return for i in range(len(nums)): self.dfs(k, nums[i+1:], temp+[nums[i]], results)