LeetCode(力扣)47.全排列 IIPython

LeetCode47.全排列 II

    • 题目链接
    • 代码

题目链接

https://leetcode.cn/problems/permutations-ii/
LeetCode(力扣)47.全排列 IIPython_第1张图片

代码

class Solution:
    def permuteUnique(self, nums):
        nums.sort()  # 排序
        result = []
        self.backtracking(nums, [], [False] * len(nums), result)
        return result

    def backtracking(self, nums, path, used, result):
        if len(path) == len(nums):
            result.append(path[:])
            return
        for i in range(len(nums)):
            if (i > 0 and nums[i] == nums[i - 1] and not used[i - 1]) or used[i]:
                continue
            used[i] = True
            path.append(nums[i])
            self.backtracking(nums, path, used, result)
            path.pop()
            used[i] = False

你可能感兴趣的:(leetcode,算法,职场和发展,python)