Skip to content

Commit bbbfef8

Browse files
committed
Update solution 047 [Python3]
1 parent 0eb086e commit bbbfef8

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution:
2+
def permuteUnique(self, nums):
3+
ans = [[]]
4+
for n in nums:
5+
new_ans = []
6+
for l in ans:
7+
for i in range(len(l)+1):
8+
new_ans.append(l[:i]+[n]+l[i:])
9+
if i<len(l) and l[i]==n: break #handles duplication
10+
ans = new_ans
11+
return ans
12+
13+
class Solution:
14+
def permute(self, nums):
15+
"""
16+
:type nums: List[int]
17+
:rtype: List[List[int]]
18+
"""
19+
if len(nums) <= 1:
20+
return [nums]
21+
ans = []
22+
for i, num in enumerate(nums):
23+
n = nums[:i] + nums[i+1:]
24+
for y in self.permute(n):
25+
ans.append([num] + y)
26+
return ans
27+
28+
def permuteUnique(self, nums):
29+
"""
30+
:type nums: List[int]
31+
:rtype: List[List[int]]
32+
"""
33+
ans=self.permute(nums)
34+
temp=[]
35+
for i in ans:
36+
if i in temp:
37+
pass
38+
else:
39+
temp.append(i)
40+
return temp

0 commit comments

Comments
 (0)