【力扣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
相关推荐
Ulyanov1 天前
构建企业级雷达电子战仿真引擎的工程化基础 第一篇:CI/CD流水线与自动化测试体系
python·ci/cd·架构·系统仿真·雷达电子战仿真
米粒11 天前
力扣算法刷题 Day 27
算法·leetcode·职场和发展
IAUTOMOBILE1 天前
Python 流程控制与函数定义:从调试现场到工程实践
java·前端·python
Fuxiao___1 天前
C 语言核心知识点讲义(循环 + 函数篇)
算法·c#
Mr_Xuhhh1 天前
LeetCode hot 100(C++版本)(上)
c++·leetcode·哈希算法
漫随流水1 天前
c++编程:反转字符串(leetcode344)
数据结构·c++·算法
TT_44191 天前
python程序实现图片截图溯源功能
开发语言·python
小陈的进阶之路1 天前
logging 日志模块笔记
python
cqbelt1 天前
Python 并发编程实战学习笔记
笔记·python·学习
穿条秋裤到处跑1 天前
每日一道leetcode(2026.03.31):字典序最小的生成字符串
算法·leetcode