Python Recursive DP and Backtracking solution sharing


  • 1
    G
    class Solution(object):
        def permute(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            if (len(nums)==1):
                return [nums]
            ret = []
            for i in range(0,len(nums)):
                sub_res = self.permute(nums[0:i]+nums[i+1:])
                ret += [sub_res_element+[nums[i]] for sub_res_element in sub_res]
            return ret
    

    class Solution(object):
        def permute(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            res = []
            self.wawa(nums,[],res)
            return(res)
            
        def wawa(self,nums,cur,res):
            if (len(nums) == 0):
                res.append(cur)
                return
            for i in range(len(nums)):
                nums[0],nums[i] = nums[i],nums[0]
                self.wawa(nums[1:], cur+[nums[0]], res)

Log in to reply
 

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