【力扣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
相关推荐
copyer_xyf42 分钟前
Agent 流程编排
后端·python·agent
copyer_xyf1 小时前
Agent RAG
后端·python·agent
copyer_xyf1 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf1 小时前
Agent 记忆管理
后端·python·agent
JieE2129 小时前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
星云穿梭17 小时前
用Python写一个带图形界面的学生管理系统——完整教程
python
Jack2017 小时前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法
金銀銅鐵17 小时前
用 Pygame 实现 15 puzzle
python·数学·游戏
小小杨树18 小时前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
黄忠1 天前
大模型之LangGraph技术体系
python·llm