首页 > 编程语言 > 详细

LeetCode | 0077. Combinations组合【Python】

时间:2020-03-05 22:35:35      阅读:60      评论:0      收藏:0      [点我收藏+]

LeetCode 0077. Combinations组合【Medium】【Python】【回溯】

Problem

LeetCode

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

Example:

Input: n = 4, k = 2
Output:
[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]

问题

力扣

给定两个整数 n 和 k,返回 1

if name == "__main... n 中所有可能的 k 个数的组合。

示例:

输入: n = 4, k = 2
输出:
[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]

思路

回溯

回溯三步骤
1.有效结果:path 长度等于 k。
2.回溯范围及答案更新。
3.剪枝条件:经过分析,可以发现 i 应该 <= n - (k - len(path)) + 1。
详细分析可以参考 liweiwei1419 的题解(在最后)。
Python3代码
class Solution:
    def combine(self, n: int, k: int) -> List[List[int]]:
        # special judgment
        if n <= 0 or k <= 0 or k > n:
            return []
        
        res = []
        self.dfs(1, k, n, [], res)
        return res
    
    def dfs(self, start, k, n, path, res):
        # 1.valid result
        if len(path) == k:
            res.append(path[:])
            return
        
        # 3.pruning
        for i in range(start, n - (k - len(path)) + 2):
            # 2.backtrack and update
            path.append(i)
            self.dfs(i + 1, k, n, path, res)
            path.pop()

代码地址

GitHub链接

参考

liweiwei1419 题解

LeetCode | 0077. Combinations组合【Python】

原文:https://www.cnblogs.com/wonz/p/12423196.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!