my python solution (dfs without recording used numbers)


  • 0
    X

    class Solution(object):
    def permute(self, nums):
    """
    :type nums: List[int]
    :rtype: List[List[int]]
    """
    answer = []
    element = []
    self.dfs(nums, element, answer)
    return answer

    def dfs(self, nums, element, answer): # without recording used numbers
        if len(element) == len(nums):
            answer.append(element[:])
            return
        else:
            for j in range(len(nums)):
                if nums[j] not in element:
                    element.append(nums[j])
                else:
                    continue
                self.dfs(nums,element,answer)
                element.pop()

Log in to reply
 

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