mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 20:40:39 +08:00
Update 0047.全排列II.md
This commit is contained in:
@ -208,28 +208,25 @@ class Solution {
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
|
def permuteUnique(self, nums):
|
||||||
# res用来存放结果
|
nums.sort() # 排序
|
||||||
if not nums: return []
|
result = []
|
||||||
res = []
|
self.backtracking(nums, [], [False] * len(nums), result)
|
||||||
used = [0] * len(nums)
|
return result
|
||||||
def backtracking(nums, used, path):
|
|
||||||
# 终止条件
|
def backtracking(self, nums, path, used, result):
|
||||||
if len(path) == len(nums):
|
if len(path) == len(nums):
|
||||||
res.append(path.copy())
|
result.append(path[:])
|
||||||
return
|
return
|
||||||
for i in range(len(nums)):
|
for i in range(len(nums)):
|
||||||
if not used[i]:
|
if (i > 0 and nums[i] == nums[i - 1] and not used[i - 1]) or used[i]:
|
||||||
if i>0 and nums[i] == nums[i-1] and not used[i-1]:
|
|
||||||
continue
|
continue
|
||||||
used[i] = 1
|
used[i] = True
|
||||||
path.append(nums[i])
|
path.append(nums[i])
|
||||||
backtracking(nums, used, path)
|
self.backtracking(nums, path, used, result)
|
||||||
path.pop()
|
path.pop()
|
||||||
used[i] = 0
|
used[i] = False
|
||||||
# 记得给nums排序
|
|
||||||
backtracking(sorted(nums),used,[])
|
|
||||||
return res
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
|
Reference in New Issue
Block a user