LeetCode Problem 47-Permutations II Posted on 2019-03-18 | In LeetCode | Visitors 全排列 II。给定一个可包含重复数字的序列,返回所有不重复的全排列。 示例: 1234567输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 思路一 回溯法。 123456789101112131415161718class Solution: def permuteUnique(self, nums: List[int]) -> List[List[int]]: def backtrack(result, tmp, nums, used): if len(tmp) == len(nums): result.append(tmp) return for i in range(len(nums)): if used[i]: continue if i > 0 and nums[i] == nums[i-1] and used[i-1]: return used[i] = True backtrack(result, tmp+[nums[i]], nums, used) used[i] = False result = [] nums.sort() backtrack(result, [], nums, [False] * len(nums)) return result 相似问题 Next Permutation Permutations Palindrome Permutation II Number of Squareful Arrays 此时不赞何时赞! 赏 微信打赏 支付宝打赏