代码随想录训练营Day29|491.递增子序列46.全排列47.全排列 II

目录

学习目标

学习内容

491.递增子序列

46.全排列

47.全排列 II


学习目标

491.递增子序列

46.全排列

47.全排列 II

学习内容

491.递增子序列

491. 递增子序列 - 力扣(LeetCode)icon-default.png?t=N3I4https://leetcode.cn/problems/non-decreasing-subsequences/

class Solution:
    def findSubsequences(self, nums: List[int]) -> List[List[int]]:
        res = []
        tmp = [-101]
        n = len(nums)
        def dfs(i):
            if len(tmp)>=3:
                res.append(tmp[1:])
            if i==n:
                return
            s =set()
            for j in range(i,n):
                if nums[j]>=tmp[-1] and nums[j] not in s:
                    s.add(nums[j])
                    tmp.append(nums[j])
                    dfs(j+1)
                    tmp.pop()
        dfs(0)
        return res

46.全排列

46. 全排列 - 力扣(LeetCode)icon-default.png?t=N3I4https://leetcode.cn/problems/permutations/

class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        res = []
        tmp = []
        n = len(nums)
        def dfs(i):
            if i==n:
                res.append(tmp.copy())
                return
            for j in range(n-i):
                t = nums[j]
                tmp.append(nums[j])
                nums.pop(j)
                dfs(i+1)
                tmp.pop()
                nums.insert(j,t)
        dfs(0)
        return res

47.全排列 II

47. 全排列 II - 力扣(LeetCode)icon-default.png?t=N3I4https://leetcode.cn/problems/permutations-ii/

class Solution:
    def permuteUnique(self, nums: List[int]) -> List[List[int]]:
        res = []
        tmp = []
        n = len(nums)
        nums.sort()
        def dfs(i):
            if i==n:
                res.append(tmp.copy())
                return
            for j in range(n-i):
                if j>0 and nums[j]==nums[j-1]:
                    continue
                t = nums[j]
                tmp.append(nums[j])
                nums.pop(j)
                dfs(i+1)
                tmp.pop()
                nums.insert(j,t)
        dfs(0)
        return res

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值