Increasing Subsequence

这道题的tag上面写的是DFS,实际上就是subsetII这道题的一个变体而已,这道题需要保证当前调用层不能出现一个相同的数两次加入备选答案中,其他地方就是套一个backtracking 模板就行

class Solution(object):
    def findSubsequences(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        tmp, res = [], []
        self.helper(0, len(nums), tmp, res, nums)
        return res

    def helper(self, start, end, tmp, res, nums):
        if len(tmp) >= 2:
            ref = tmp[:]
            res.append(ref)

        if start >= end:
            return 

        store = {}
        for i in xrange(start, end):
            if (not tmp or tmp[-1] <= nums[i]) and nums[i] not in store:
                store[nums[i]] = 1
                tmp.append(nums[i])
                self.helper(i + 1, end, tmp, res, nums)
                tmp.pop()

results matching ""

    No results matching ""