【力扣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
相关推荐
前端小L40 分钟前
贪心算法专题(十):维度权衡的艺术——「根据身高重建队列」
javascript·算法·贪心算法
方得一笔40 分钟前
自定义常用的字符串函数(strlen,strcpy,strcmp,strcat)
算法
秃了也弱了。42 分钟前
python实现定时任务:schedule库、APScheduler库
开发语言·python
Dfreedom.1 小时前
从 model(x) 到__call__:解密深度学习框架的设计基石
人工智能·pytorch·python·深度学习·call
weixin_425023001 小时前
Spring Boot 配置文件优先级详解
spring boot·后端·python
Xの哲學1 小时前
Linux SMP 实现机制深度剖析
linux·服务器·网络·算法·边缘计算
wuk9982 小时前
使用PCA算法进行故障诊断的MATLAB仿真
算法·matlab
额呃呃2 小时前
二分查找细节理解
数据结构·算法
无尽的罚坐人生2 小时前
hot 100 283. 移动零
数据结构·算法·双指针
永远都不秃头的程序员(互关)2 小时前
C++动态数组实战:从手写到vector优化
c++·算法