【力扣TOP100】全排列

题目要求:

思路:

可使用递归的方式。permute(nums)=对permute(nums[0:len(nums)-1])的每一个元素,尝试添加元素nums[len(nums)-1]

代码:

python 复制代码
class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        l=len(nums)
        if l==1:
            return [nums]
        t=self.permute(nums[0:l-1])
        ans=[]
        for v in t:
            for j in range(l-1):
                tt=v[0:j]+[nums[l-1]]+v[j:l-1]
                ans.append(tt)
            ans.append(v+[nums[l-1]])
        return ans
相关推荐
databook3 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室4 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三5 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户2519162427118 小时前
Python之语言特点
python
刘立军9 小时前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
聚客AI10 小时前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
数据智能老司机12 小时前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
大怪v13 小时前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
数据智能老司机13 小时前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i15 小时前
django中的FBV 和 CBV
python·django