【力扣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
相关推荐
aqiu11111127 分钟前
【C++算法打怪专栏】8. 字符串转换整数 (atoi)
开发语言·c++·算法
迅猛龙办公室1 小时前
python实现倒序输出数字
开发语言·python
信奥卷王9 小时前
[GESP202609 六级] 分树规划
算法
夏幻灵9 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
全栈练习生10 小时前
AI Agent 沙箱
python·ai
徐小黑ACG10 小时前
Golang 基础05 结构体struct
开发语言·算法·golang
Navigator_Z11 小时前
LeetCode //C - 1286. Iterator for Combination
c语言·算法·leetcode
SEO_juper11 小时前
用 Python 写一个 GEO 可见性检查脚本:你的网站现在能被 AI 引用吗
开发语言·人工智能·爬虫·python·seo·外贸独立站
北极有牛11 小时前
cpp学习笔记--常量指针
java·开发语言·算法