-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path47.全排列-ii.py
More file actions
46 lines (46 loc) · 985 Bytes
/
47.全排列-ii.py
File metadata and controls
46 lines (46 loc) · 985 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#
# @lc app=leetcode.cn id=47 lang=python
#
# [47] 全排列 II
#
# https://leetcode-cn.com/problems/permutations-ii/description/
#
# algorithms
# Medium (53.97%)
# Likes: 159
# Dislikes: 0
# Total Accepted: 22.6K
# Total Submissions: 41.6K
# Testcase Example: '[1,1,2]'
#
# 给定一个可包含重复数字的序列,返回所有不重复的全排列。
#
# 示例:
#
# 输入: [1,1,2]
# 输出:
# [
# [1,1,2],
# [1,2,1],
# [2,1,1]
# ]
#
#
class Solution(object):
def permuteUnique(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
def backtrack(nums,path):
if not nums:
res.append(path)
return
for i in xrange(len(nums)):
if i>=1 and nums[i]==nums[i-1]:
continue
backtrack(nums[:i]+nums[i+1:],path+[nums[i]])
res=[]
nums.sort()
backtrack(nums,[])
return res