【LeetCode】46,47 Daily temperature. Difficulty Level: Moderate. Understanding Search + Backtracking

1. Full permutation of arrays without repeated numbers

1.1 Topics

Given an array nums without repeated numbers, return all possible full permutations of it. You can return answers in any order.

Example 1:

输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

Example 2:

输入:nums = [1]
输出:[[1]]

1.2 Recursive method

code:

class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        def backtrack(subArray,curArray):
            if not subArray:
                ans.append(curArray)
                return
            else:
                for i in range(len(subArray)):
                    backtrack(subArray[:i]+subArray[i+1:],curArray+[subArray[i]])
        ans=[]
        backtrack(nums,[])
        return ans

1.3 Classic method: search + backtracking

code:

class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        def dfs(nums, path):
            if not nums:
                ans.append(path)
                return
            nums_copy = copy.deepcopy(nums)
            for num in nums_copy:
                nums.remove(num)
                dfs(nums, path + [num])
                nums.append(num)
        ans =[]
        dfs(nums,[])
        return ans  

2. Full Permutation of Arrays Containing Repeating Numbers

2.1 Topic

Given a sequence nums that may contain repeating numbers, return all non-repeating full permutations in any order.

Example 1:

输入:nums = [1,1,2]
输出:[[1,1,2], [1,2,1], [2,1,1]]

Example 2:

输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

2.1 Rough solution: first arrange the whole and then remove the duplicates, the time complexity is high

The simplest idea is to add a step to the code of 1.2 to determine whether the path is in ans, but the time complexity of determining whether a list is in another list is very high, resulting in a long running time, which may exceed the time limit when there are more test cases.

code:

class Solution:
    def permuteUnique(self, nums: List[int]) -> List[List[int]]:
        def backtrack(nums,path):
            if not nums:
                if path not in ans:
                    ans.append(path)
                return
            else:
                for i in range(len(nums)):
                    backtrack(nums[:i]+nums[i+1:],path+[nums[i]])
        ans=[]
        backtrack(nums,[])
        return ans

Results of the:

执行用时:736 ms, 在所有 Python3 提交中击败了 11.89% 的用户
内存消耗:16.4 MB, 在所有 Python3 提交中击败了 21.76% 的用户

2.2 Exquisite solution: first remove the duplicates and then arrange them in full, search + backtracking + pruning

code:

class Solution:
    def permuteUnique(self, nums: List[int]) -> List[List[int]]:
        def dfs(nums, path):
            if not nums:
                ans.append(path)
                return
            temp = list(set(nums))
            for num in temp:
                nums.remove(num)
                dfs(nums, path + [num])
                nums.append(num)
        ans=[]
        dfs(nums,[])
        return ans

Results of the:

执行用时:56 ms, 在所有 Python3 提交中击败了 47.70% 的用户
内存消耗:16.5 MB, 在所有 Python3 提交中击败了 20.45% 的用户

Guess you like

Origin blog.csdn.net/qq_43799400/article/details/131744374