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