File tree 1 file changed +40
-0
lines changed
solution/047.Permutations II
1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments