We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 4ff4830 commit 43a46e4Copy full SHA for 43a46e4
kotlin/0047-permutations-ii.kt
@@ -0,0 +1,31 @@
1
+class Solution {
2
+ fun permuteUnique(nums: IntArray): List<List<Int>> {
3
+ val count = HashMap<Int, Int>()
4
+ val permut = LinkedList<Int>()
5
+ val res = LinkedList<LinkedList<Int>>()
6
+
7
+ for (n in nums) count[n] = count.getOrDefault(n, 0) + 1
8
9
+ fun backtrack() {
10
+ if (permut.size == nums.size) {
11
+ res.add(LinkedList(permut))
12
+ return
13
+ }
14
15
+ for (n in count.keys) {
16
+ if (count[n]!! > 0) {
17
+ permut.addLast(n)
18
+ count[n] = count[n]!! - 1
19
20
+ backtrack()
21
22
+ count[n] = count[n]!! + 1
23
+ permut.removeLast()
24
25
26
27
28
29
+ return res
30
31
+}
0 commit comments