BackTracking Python


  • 0
    class Solution(object):
        def permute(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            answer = []
            rec = [0 for i in xrange(len(nums))]
            def dfs(ans):
                if len(ans) == len(nums):
                    answer.append(ans[:])
                    return
                for i in xrange(len(nums)):
                    if rec[i] != 1:
                        rec[i] = 1
                        dfs(ans + [nums[i]])
                        rec[i] = 0
            dfs([])
            return answer
    

Log in to reply
 

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