Skip to content

491. Increasing Subsequences #86

@tech-cow

Description

@tech-cow

第一刷

'''
Bug 1: 这里如果 == base case底端,很多过程中的case都不会被记录
if index == len(nums) and len(temp) >= 2:

疑问:
为什么会有[4,7,7]这个output,set不会把第二个7去重么?

答:
每层递归的都会开一次新的set,而不是globalSet。所以只会针对当前层的duplicate
比如arr = [4,7,7]
    arr[1]是第二层的
    arr[2]是第三层的,到达第三层的时候,新开辟的set里面不会有第二层的7
'''
    
class Solution(object):
    def findSubsequences(self, nums):
        res = []
        self.dfsHelper(nums, [], 0, res)
        return res
    
    
    def dfsHelper(self, nums, temp, index, res):
        # Bug 1
        if index <= len(nums) and len(temp) >= 2:
            res.append(temp[:])

        visited = set()
        for i in range(index, len(nums)):
            if temp and temp[-1] > nums[i]: continue
            if nums[i] in visited: continue
            visited.add(nums[i])
            
            temp.append(nums[i])
            self.dfsHelper(nums, temp, i + 1, res)
            temp.pop()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy