leetcode-77-组合 题解

该博客介绍了如何利用Python的itertools库来解决LeetCode中的组合问题。提供了使用combinations、combinations_with_replacement、permutations和product四个函数的示例,分别对应不放回、有放回的组合和排列,以及笛卡尔积的计算。通过这些函数,可以简洁地生成指定长度的数列组合和排列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

给定两个整数 n 和 k,返回 1 … n 中所有可能的 k 个数的组合。

示例

  • 示例 1:
    输入: n = 4, k = 2
    输出:
    [
    [2,4],
    [3,4],
    [2,3],
    [1,2],
    [1,3],
    [1,4],
    ]
    
    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/robot-return-to-origin

  • 解题思路

    虽然是中等题,但是使用python内置函数,就简单了。

    • 组合,没有重复的情况(不放回抽样组合) 使用 itertools.combinations 方法,

          def combine(self, n: int, k: int) -> List[List[int]]:
              result = []
              for i in itertools.combinations(list(range(1, n+1)), k):
                  result.append(list(i))
              return result
              # [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]] 6
      

    其它方法:

    • 组合,有重复的情况(放回抽样组合) 使用 itertools.combinations_with_replacement 方法,

          def combine(self, n: int, k: int) -> List[List[int]]:
              result = []
              for i in itertools.combinations_with_replacement(list(range(1, n+1)), k):
                  result.append(list(i))
              return result
              # [[1, 1], [1, 2], [1, 3], [1, 4], [2, 2], [2, 3], [2, 4], [3, 3], [3, 4], [4, 4]] 10
      
    • 排列 (不放回抽样排列)使用 itertools.permutations 方法,

          def combine(self, n: int, k: int) -> List[List[int]]:
              result = []
              for i in itertools.permutations(list(range(1, n+1)), k):
                  result.append(list(i))
              return result
              # [[1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4, 2], [4, 3]] 12
      
    • 笛卡尔积 (有放回抽样排列) 使用 itertools.product 方法,

          def combine(self, n: int, k: int) -> List[List[int]]:
              result = []
              for i in itertools.product(list(range(1, n+1)), repeat = k):
                  result.append(list(i))
              return result
              # [[1, 1], [1, 2], [1, 3], [1, 4], [2, 1], [2, 2], [2, 3], [2, 4], [3, 1], [3, 2], [3, 3], [3, 4], [4, 1], [4, 2], [4, 3], [4, 4]] 16
      
      

    combinationspermutations返回的是对象地址, 需要将iterator 转换成list 即可

  • 题解1:

    执行用时:48 ms, 在所有 Python3 提交中击败了95.61%的用户

    内存消耗:14.8 MB, 在所有 Python3 提交中击败了98.26%的用户

    import itertools
    from typing import List
    
    class Solution:
        def combine(self, n: int, k: int) -> List[List[int]]:
            result = []
            for i in itertools.combinations(list(range(1, n+1)), k):
                result.append(list(i))
            return result
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Spaceack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值