class Solution(object): def permuteUnique(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ result = [] nums.sort() # 排序 return self.get_permute(nums)
def get_permute(self, nums): if len(nums) <= 1: return [nums] ans = [] for i, num in enumerate(nums): if i == 0 or nums[i] != nums[i - 1]: # 加一行对比前一个数 n = nums[:i] + nums[i+1:] # n是剩余数的list for y in self.get_permute(n): # 直到函数有return,一个数的时候[nums],所以y是list ans.append([num] + y) return ans