LeetCode(力扣)491. 递增子序列Python

LeetCode491. 递增子序列

    • 题目链接
    • 代码

题目链接

https://leetcode.cn/problems/non-decreasing-subsequences/
LeetCode(力扣)491. 递增子序列Python_第1张图片

代码

class Solution:
    def backtracking(self, nums, index, result, path):
        if len(path) > 1:
            result.append(path[:])
        uset = set()
        for i in range(index, 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, result, path)
            path.pop()

    def findSubsequences(self, nums: List[int]) -> List[List[int]]:
        result = []
        self.backtracking(nums, 0, result, [])
        return result

你可能感兴趣的:(leetcode,python,算法,职场和发展)