Python backtracking solution(increasing last_pick index)


  • 0

    '''
    class Solution(object):
    def subsets(self, nums):
    """
    :type nums: List[int]
    :rtype: List[List[int]]
    """
    r = [[]]
    for i in range(1, len(nums) + 1):
    temp_r = []
    temp = []
    last_pick = -1
    self.dfs(nums, temp_r, temp, 0, i, last_pick)
    for j in temp_r:
    r.append([k for k in j])
    return r

    def dfs(self, nums, temp_r, temp, index, n, last_pick):
        if index == n:
            temp = sorted(temp)
            if temp not in temp_r:
                temp_r.append(temp)
        else:
            for i in range(last_pick + 1, len(nums)):
                if index >= len(temp):
                    temp.append(nums[i])
                else:
                    temp[index] = nums[i]
                self.dfs(nums, temp_r, temp, index + 1, n, i)
    

    '''


Log in to reply
 

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