forked from javadev/LeetCode-in-Kotlin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolution.kt
41 lines (36 loc) · 1.12 KB
/
Solution.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package g0001_0100.s0047_permutations_ii
// #Medium #Array #Backtracking #Algorithm_II_Day_10_Recursion_Backtracking
// #2023_07_05_Time_199_ms_(100.00%)_Space_39.1_MB_(92.98%)
class Solution {
private var ans: MutableList<List<Int>>? = null
fun permuteUnique(nums: IntArray): List<List<Int>> {
ans = ArrayList()
permute(nums, 0)
return ans as ArrayList<List<Int>>
}
private fun permute(nums: IntArray, p: Int) {
if (p >= nums.size - 1) {
val t: MutableList<Int> = ArrayList(nums.size)
for (n in nums) {
t.add(n)
}
ans!!.add(t)
return
}
permute(nums, p + 1)
val used = BooleanArray(30)
for (i in p + 1 until nums.size) {
if (nums[i] != nums[p] && !used[10 + nums[i]]) {
used[10 + nums[i]] = true
swap(nums, p, i)
permute(nums, p + 1)
swap(nums, p, i)
}
}
}
private fun swap(nums: IntArray, i: Int, j: Int) {
val t = nums[i]
nums[i] = nums[j]
nums[j] = t
}
}