What's the problem of my codes

  • 0
    def findSubsequences(self, nums):
            :type nums: List[int]
            :rtype: List[List[int]]
            def dfs(nums, path, res):
                if len(path) > 1: 
                if len(nums) == 0: return 
                for i in range(len(nums)):
                    if i > 0 and nums[i] == nums[i-1]: continue
                    if (len(path) > 0 and path[-1] <= nums[i]) or len(path) == 0:
                        dfs(nums[i+1:], path+[nums[i]], res)
            res = []
            dfs(nums, [], res)
            return res

  • 0

    I noticed that you are trying to avoid duplicates by checking nums[i] == nums[i - 1] and skipping the loop if true. This only works if the array is sorted, which isn't always true for this problem. It is much easier to use a set instead to solve this problem :)

  • 1

    @simonzhu91 Exactly! For this problem, I need to check all previous numbers handling duplicates not only just one. Using set also save time. Thank you!

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.