Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0c02c76

Browse files
committed
Time: 9 ms (78.22%), Space: 38.8 MB (56.69%) - LeetHub
1 parent b0aaffa commit 0c02c76

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
private fun backtrack(
3+
nums: IntArray,
4+
result: MutableList<List<Int>>,
5+
used: BooleanArray,
6+
current: MutableList<Int>,
7+
) {
8+
if (current.size == nums.size) {
9+
result += current
10+
return
11+
}
12+
for (i in nums.indices) {
13+
if (used[i]) {
14+
continue
15+
}
16+
17+
used[i] = true
18+
val next = current.toMutableList()
19+
next += nums[i]
20+
backtrack(nums, result, used, next)
21+
used[i] = false
22+
}
23+
}
24+
25+
fun permute(nums: IntArray): List<List<Int>> {
26+
val result = mutableListOf<List<Int>>()
27+
val used = BooleanArray(nums.size)
28+
29+
backtrack(nums, result, used, mutableListOf())
30+
return result
31+
}
32+
}

0 commit comments

Comments
 (0)