【力扣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
相关推荐
weixin_307779132 小时前
Redis Windows迁移方案与测试
c++·windows·redis·算法·系统架构
麦兜*4 小时前
Spring Boot集成方案 + Elasticsearch向量检索,语义搜索核弹
java·spring boot·python·spring·elasticsearch·spring cloud·系统架构
仪器科学与传感技术博士4 小时前
python:讲懂决策树,为理解随机森林算法做准备,以示例带学习,通俗易懂,容易理解和掌握
python·算法·决策树
歪歪1004 小时前
HTML 如何转 Markdown
开发语言·chrome·python·程序人生·html
小指纹4 小时前
cf--思维训练
c++·算法·macos·ios·objective-c·cocoa
小指纹4 小时前
河南萌新联赛2025第(四)场【补题】
数据结构·c++·算法·macos·objective-c·cocoa·图论
菜鸟555554 小时前
河南萌新联赛2025第四场-河南大学
c++·算法·思维·河南萌新联赛
王者鳜錸4 小时前
PYTHON从入门到实践-18Django模版渲染
开发语言·python·django
F_D_Z5 小时前
【感知机】感知机(perceptron)模型与几何解释
学习·算法·支持向量机
竹子_235 小时前
《零基础入门AI:传统机器学习进阶(从拟合概念到K-Means算法)》
人工智能·算法·机器学习