【力扣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
相关推荐
醒过来摸鱼1 天前
Java classloader
java·开发语言·python
superman超哥1 天前
仓颉语言中元组的使用:深度剖析与工程实践
c语言·开发语言·c++·python·仓颉
小鸡吃米…1 天前
Python - 继承
开发语言·python
祁思妙想1 天前
Python中的FastAPI框架的设计特点和性能优势
开发语言·python·fastapi
LYFlied1 天前
【每日算法】LeetCode 153. 寻找旋转排序数组中的最小值
数据结构·算法·leetcode·面试·职场和发展
唐装鼠1 天前
rust自动调用Deref(deepseek)
开发语言·算法·rust
Dingdangcat861 天前
反恐精英角色识别与定位-基于改进的boxinst_r101_fpn_ms-90k_coco模型实现
python
世界唯一最大变量1 天前
利用自定义积分公式,目前可以求出所有1元方程和1元积分的近似值
python
写代码的【黑咖啡】1 天前
深入理解 Python 中的模块(Module)
开发语言·python
ytttr8731 天前
MATLAB基于LDA的人脸识别算法实现(ORL数据库)
数据库·算法·matlab