【力扣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
相关推荐
圣光SG3 小时前
Java操作题练习(七)
java·开发语言·算法
_Jimmy_4 小时前
Agent 溯源精度提升方案
人工智能·python·langchain
Cx330_FCQ5 小时前
Tmux使用
服务器·git·算法
SamChan905 小时前
在Web应用中集成PDF多语言翻译功能:PDFTranslator API实战指南
前端·python·ai·pdf·yapi·机器翻译
天天进步20156 小时前
Python全栈项目--智能办公自动化系统
开发语言·python
拳里剑气6 小时前
C++算法:多源BFS
c++·算法·宽度优先·多源bfs
颜酱6 小时前
09 | 重构项目结构
人工智能·python·langchain
ysa0510307 小时前
【板子】短序列dp(换成维护更小常数维度的dp)
c++·笔记·算法·板子
shwill1237 小时前
PID 算法(三)--- 增量 PID ↔ 单神经元 PID 等价映射
linux·算法
kisloy7 小时前
【爬虫入门第8讲】Python爬虫反爬入门
开发语言·爬虫·python