【力扣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
相关推荐
王仔小奶18 分钟前
慢SQL治理与性能调优:DBA如何用Shell/Python提升运维效率?
python·sql·dba
用户03321266636722 分钟前
使用 Python 快速在 PowerPoint 中生成表格【教程】
python
邵奈一28 分钟前
GPT-6.1 Terra速度是Sol的3倍,价格减半
算法
RaaS10028 分钟前
Node.js Brotli RCE 漏洞自查指南:fetch、undici、axios 都在影响面,三分钟确认你的版本
人工智能·深度学习·算法
夜晚回家35 分钟前
Python 身份运算符详解 | is 与 == 到底差在哪
python
fengye20716138 分钟前
cpython框架结构
python
shehuiyuelaiyuehao42 分钟前
算法56,重排链表
数据结构·算法·链表
用户91294046848421 小时前
大模型返回 JSON 后,先过这道 Python 校验
python
Dmz丶1 小时前
前端转 AI 100 天 Day 32:LLM 客户端工程化——重试、超时、降级、限流
python