491. 递增子序列

Medium

思路

尝试写一下代码,AC!

最后应为有重复节点,这边选择了再最后去重,也可以在递归中用一个变量记录走过的点,如果遇到相同的进行剪枝,在过程中去重

代码

python3

class Solution:
    def helper(self, nums, curIndex, path, res):
      if curIndex >= len(nums):
        return 
 
      if len(path) > 1:
        res.append(path)

      for i in range(curIndex + 1,len(nums)):
        if curIndex == -1 or nums[i] >= nums[curIndex]:
          path.append(nums[i])
          self.helper(nums, i, path[::], res)
          path.pop()
      return

    def findSubsequences(self, nums: List[int]) -> List[List[int]]:
      res = []
      self.helper(nums, -1, [], res)
      return list(set([tuple(x) for x in res]))