【力扣TOP100】全排列

题目要求:

思路:

可使用递归的方式。permute(nums)=对permute(nums0:len(nums)-1)的每一个元素,尝试添加元素numslen(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
相关推荐
Nil20811 分钟前
leetcode 138随机链表的复制
算法·leetcode·链表
zzzzzz3101 小时前
AI 助手最危险的不是报错,而是“看起来成功”:我给工作流加了四道保险
人工智能·python
用户8356290780511 小时前
如何使用 Python 将 PowerPoint 转换为 PDF 文档
后端·python
菜冻鱼1 小时前
Python-pytorch-高级技巧
开发语言·人工智能·pytorch·python·深度学习·神经网络·聚类
lpfasd1231 小时前
python webview打包版卡死、开发版正常
开发语言·python
用户8356290780511 小时前
如何使用 Python 为 PowerPoint 幻灯片添加切换效果
后端·python
菜冻鱼2 小时前
Python-pytorch-模型保存与加载
开发语言·人工智能·pytorch·python·深度学习·机器学习
亿牛云爬虫专家2 小时前
爬虫调度系统设计:用 Celery 实现按媒体权重动态调整的抓取频率控制
爬虫·python·隧道代理·舆情采集·媒体权重·频率控制·ip自动轮换
疯狂打码的少年2 小时前
【数据结构】图的遍历:深度优先搜索(DFS)
数据结构·笔记·算法·深度优先
Code额3 小时前
Python 连接 DeepSeek API,OpenAI
开发语言·python·ai·ai编程