Typical python DFS solution. Faster than 80.36%


  • 0

    Not very smart Python solution with DFS. I wonder why it is faster than 80.36% python submissions... Run time O(n!n)

    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        used = set({})
        current = []
        solutions = []
        self.dfs(nums, used, current, solutions)
        return solutions
        
    def dfs(self, nums, used, current, solutions):
        if len(used) == len(nums):
            solutions.append(current[:])
            return
        for i in range(len(nums)):
            if i not in used:
                used.add(i)
                current.append(nums[i])
                self.dfs(nums, used, current, solutions)
                current.pop()
                used.remove(i)

Log in to reply
 

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