-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_20220803.kt
More file actions
124 lines (106 loc) · 3.48 KB
/
_20220803.kt
File metadata and controls
124 lines (106 loc) · 3.48 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
@file:Suppress("DuplicatedCode", "ClassName", "ObjectPropertyName")
package _2022._08
import Testable
import java.util.*
// 46. Permutations
// https://leetcode.com/problems/permutations/
private interface Leetcode_46 {
fun permute(nums: IntArray): List<List<Int>>
companion object : Testable {
override fun test() {
}
}
// Runtime: 225 ms, faster than 96.25% of Kotlin online submissions for Permutations.
// Memory Usage: 37.9 MB, less than 95.97% of Kotlin online submissions for Permutations.
// https://leetcode.com/submissions/detail/763899810/
private class M1 : Leetcode_46 {
private val res = ArrayList<ArrayList<Int>>()
private val cur = ArrayList<Int>()
override fun permute(nums: IntArray): List<List<Int>> {
val candidates = ArrayList<Int>()
for (n in nums) {
candidates.add(n)
}
dfs(candidates)
return res
}
fun dfs(candidates: ArrayList<Int>) {
if (candidates.isEmpty()) {
res.add(ArrayList(cur))
return
}
for (i in candidates.indices) {
val n = candidates[i]
candidates.removeAt(i)
cur += n
dfs(candidates)
candidates.add(i, n)
cur.removeAt(cur.lastIndex)
}
}
}
}
// 90. Subsets II
// https://leetcode.com/problems/subsets-ii/
private interface Leetcode_90 {
fun subsetsWithDup(nums: IntArray): List<List<Int>>
companion object : Testable {
override fun test() {
/*
val tests = listOf(
)
listOf(
M1()::,
).runTimedTests(tests) { a, b ->
invoke(a).assertEqualTo(b)
}
*/
}
}
private class M1 : Leetcode_90{
private val res: MutableList<List<Int>> = LinkedList()
private val track = LinkedList<Int>()
override fun subsetsWithDup(nums: IntArray): List<List<Int>> {
Arrays.sort(nums)
backtrack(nums, 0)
return res
}
private fun backtrack(nums: IntArray, start: Int) {
res.add(LinkedList(track))
for (i in start until nums.size) {
if (i > start && nums[i] == nums[i - 1]) {
continue
}
track.addLast(nums[i])
backtrack(nums, i + 1)
track.removeLast()
}
}
}
private class S1 : Leetcode_90 {
private val res: MutableList<List<Int>> = LinkedList()
private val track = LinkedList<Int>()
override fun subsetsWithDup(nums: IntArray): List<List<Int>> {
// 先排序,让相同的元素靠在一起
Arrays.sort(nums)
backtrack(nums, 0)
return res
}
fun backtrack(nums: IntArray, start: Int) {
// 前序位置,每个节点的值都是一个子集
res.add(LinkedList(track))
for (i in start until nums.size) {
// 剪枝逻辑,值相同的相邻树枝,只遍历第一条
if (i > start && nums[i] == nums[i - 1]) {
continue
}
track.addLast(nums[i])
backtrack(nums, i + 1)
track.removeLast()
}
}
}
}
val _20220803 = listOf<Testable>(
Leetcode_46,
)