leetcode递增子序列、排列

491.递增子序列

给定一个整型数组, 你的任务是找到所有该数组的递增子序列,递增子序列的长度至少是2。

示例:

输入: [4, 6, 7, 7]
输出: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]
说明:

给定数组的长度不会超过15。
数组中的整数范围是 [-100,100]。
给定数组中可能包含重复数字,相等的数字应该被视为递增的一种情况。

回溯 利用set去重

class Solution:
    def findSubsequences(self, nums):
        result = []
        path = []
        self.backtracking(nums, 0, path, result)
        return result
    
    def backtracking(self, nums, startIndex, path, result):
        if len(path) > 1:
            result.append(path[:])  # 注意要使用切片将当前路径的副本加入结果集
            # 注意这里不要加return,要取树上的节点
        
        uset = set()  # 使用集合对本层元素进行去重
        for i in range(startIndex, len(nums)):
            if (path and nums[i] < path[-1]) or nums[i] in uset:
                continue
            
            uset.add(nums[i])  # 记录这个元素在本层用过了,本层后面不能再用了
            path.append(nums[i])
            self.backtracking(nums, i + 1, path, result)
            path.pop()

46.全排列

力扣题目链接(opens new window)

给定一个 没有重复 数字的序列,返回其所有可能的全排列。

示例:

输入: [1,2,3]
输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ]

class Solution:
    def permute(self, nums):
        result = []
        self.backtracking(nums, [], [False] * len(nums), result)
        return result

    def backtracking(self, nums, path, used, result):
        if len(path) == len(nums):
            result.append(path[:])
            return
        for i in range(len(nums)):
            if used[i]:
                continue
            used[i] = True
            path.append(nums[i])
            self.backtracking(nums, path, used, result)
            path.pop()
            used[i] = False

47.全排列 II

给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。

示例 1:

输入:nums = [1,1,2]
输出: [[1,1,2], [1,2,1], [2,1,1]]
示例 2:

输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
提示:

1 <= nums.length <= 8
-10 <= nums[i] <= 10

class Solution:
    def permuteUnique(self, nums):
        nums.sort()  # 排序
        result = []
        self.backtracking(nums, [], [False] * len(nums), result)
        return result

    def backtracking(self, nums, path, used, result):
        if len(path) == len(nums):
            result.append(path[:])
            return
        for i in range(len(nums)):
            if (i > 0 and nums[i] == nums[i - 1] and not used[i - 1]) or used[i]:
                continue
            used[i] = True
            path.append(nums[i])
            self.backtracking(nums, path, used, result)
            path.pop()
            used[i] = False

相关推荐

  1. leetcode递增序列排列

    2023-12-29 14:40:02       30 阅读
  2. LeetCode-非递增序列

    2023-12-29 14:40:02       12 阅读
  3. LeetCode 300 最长递增序列

    2023-12-29 14:40:02       39 阅读
  4. Leetcode 300 最长递增序列

    2023-12-29 14:40:02       32 阅读
  5. LeetCode-300.最长递增序列

    2023-12-29 14:40:02       14 阅读
  6. LeetCode 300. 最长递增序列

    2023-12-29 14:40:02       7 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-29 14:40:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-29 14:40:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-29 14:40:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-29 14:40:02       18 阅读

热门阅读

  1. React函数组件使用Effect Hook(副作用钩子)

    2023-12-29 14:40:02       32 阅读
  2. react父组件props变化的时候子组件怎么监听?

    2023-12-29 14:40:02       38 阅读
  3. 【linux系统安装部署私有化的GitLab】

    2023-12-29 14:40:02       32 阅读
  4. 解除mobaxterm会话14个限制

    2023-12-29 14:40:02       36 阅读
  5. 【Qt】Qt中通过QProcess::execute()调用echo命令不生效

    2023-12-29 14:40:02       31 阅读
  6. 通配符和正则表达式

    2023-12-29 14:40:02       41 阅读
  7. Vue3 教程

    2023-12-29 14:40:02       36 阅读
  8. C++高级-模板详解

    2023-12-29 14:40:02       32 阅读
  9. 《Webpack5 升级》- Vue2.x 组件库 Webpack3 升 5

    2023-12-29 14:40:02       40 阅读
  10. udp异步方式接收消息

    2023-12-29 14:40:02       36 阅读