Skip to content

Commit 43a46e4

Browse files
authored
Create 0047-permutations-ii.kt
1 parent 4ff4830 commit 43a46e4

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Diff for: kotlin/0047-permutations-ii.kt

+31
Original file line numberDiff line numberDiff line change
@@ -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+
backtrack()
29+
return res
30+
}
31+
}

0 commit comments

Comments
 (0)