Skip to content

Commit

Permalink
Kotlin: 46. Permutations
Browse files Browse the repository at this point in the history
  • Loading branch information
MaratKhakim committed Sep 10, 2022
1 parent 2e5e56a commit 2fbabf2
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions kotlin/46-Permutations.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
fun permute(nums: IntArray): List<List<Int>> {
val res = mutableListOf<List<Int>>()
permute(nums, mutableSetOf<Int>(), mutableListOf<Int>(), res)
return res
}

fun permute(nums: IntArray, set: MutableSet<Int>, list: MutableList<Int>, res: MutableList<List<Int>>) {
if (list.size == nums.size) {
res.add(ArrayList(list))
return
}

for (i in 0..nums.size-1) {
if (!set.contains(nums[i])) {
list.add(nums[i])
set.add(nums[i])
permute(nums, set, list, res)
list.removeAt(list.size-1)
set.remove(nums[i])
}
}
}
}

0 comments on commit 2fbabf2

Please sign in to comment.