Python | Leetcode Python题解之第46题全排列

题目:

题解:

python 复制代码
class Solution:
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        def backtrack(first = 0):
            # 所有数都填完了
            if first == n:  
                res.append(nums[:])
            for i in range(first, n):
                # 动态维护数组
                nums[first], nums[i] = nums[i], nums[first]
                # 继续递归填下一个数
                backtrack(first + 1)
                # 撤销操作
                nums[first], nums[i] = nums[i], nums[first]
        
        n = len(nums)
        res = []
        backtrack()
        return res
相关推荐
青春不朽5128 分钟前
Scrapy框架入门指南
python·scrapy
MZ_ZXD00132 分钟前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php
TracyCoder1231 小时前
LeetCode Hot100(34/100)——98. 验证二叉搜索树
算法·leetcode
全栈老石1 小时前
Python 异步生存手册:给被 JS async/await 宠坏的全栈工程师
后端·python
梨落秋霜1 小时前
Python入门篇【模块/包】
python
阔皮大师2 小时前
INote轻量文本编辑器
java·javascript·python·c#
小法师爱分享2 小时前
StickyNotes,简单便签超实用
java·python
深蓝电商API2 小时前
处理字体反爬:woff字体文件解析实战
爬虫·python
开源技术2 小时前
Claude Opus 4.6 发布,100万上下文窗口,越贵越好用
人工智能·python
We་ct3 小时前
LeetCode 56. 合并区间:区间重叠问题的核心解法与代码解析
前端·算法·leetcode·typescript