Skip to content

Instantly share code, notes, and snippets.

@ChongyeWang
Created March 15, 2019 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChongyeWang/1bdd9b08cd2c8bb0e0668f96b5cbcc26 to your computer and use it in GitHub Desktop.
Save ChongyeWang/1bdd9b08cd2c8bb0e0668f96b5cbcc26 to your computer and use it in GitHub Desktop.
class Solution(object):
def permuteUnique(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums = sorted(nums)
temp = []
result = []
used = [0 for _ in range(len(nums))]
self.backtracking(temp, result, nums, used)
return result
def backtracking(self, temp, result, nums, used):
if(len(temp) == len(nums)):
result.append(temp[:])
for i in range(0, len(nums)):
if used[i] == 1: continue
if i > 0 and nums[i] == nums[i - 1] and used[i - 1] == 0: continue
used[i] = 1
temp.append(nums[i])
self.backtracking(temp, result, nums, used)
temp.pop()
used[i] = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment