-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_20220607.kt
More file actions
254 lines (217 loc) · 7.59 KB
/
_20220607.kt
File metadata and controls
254 lines (217 loc) · 7.59 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
@file:Suppress("DuplicatedCode", "ClassName")
package _2022._06
import utils.*
fun main() {
// Leetcode_21.test()
// Leetcode_141.test()
Leetcode_142.test()
}
// https://leetcode.com/problems/merge-two-sorted-lists/
private interface Leetcode_21 {
companion object {
// execution for _2022._06.Leetcode_21.My1.mergeTwoLists finished, took 151088 Nanoseconds
// execution for _2022._06.Leetcode_21.Sample1.mergeTwoLists finished, took 445839 Nanoseconds
fun test() = listOf(
My1()::mergeTwoLists,
Sample1()::mergeTwoLists,
).runTimedTests {
invoke(
linkedNodesOf(1,2,4),
linkedNodesOf(1,3,4)
).assertEqualTo(
linkedNodesOf(1,1,2,3,4,4)
)
invoke(
linkedNodesOf(1),
linkedNodesOf(2)
).assertEqualTo(
linkedNodesOf(1,2)
)
}
}
fun mergeTwoLists(list1: ListNode?, list2: ListNode?): ListNode?
// Runtime: 246 ms, faster than 59.29% of Kotlin online submissions for Merge Two Sorted Lists.
// Memory Usage: 36.3 MB, less than 23.59% of Kotlin online submissions for Merge Two Sorted Lists.
// https://leetcode.com/submissions/detail/716383804/
private class My1 : Leetcode_21 {
override fun mergeTwoLists(list1: ListNode?, list2: ListNode?): ListNode? {
if (list1 == null) return list2
if (list2 == null) return list1
var p1: ListNode? = list1
var p2: ListNode? = list2
val dummy = ListNode(-1)
var head: ListNode? = dummy
while (true) {
val v1 = p1?.`val`
val v2 = p2?.`val`
when {
v1 == null && v2 == null -> break
v1 == null -> {
head!!.next = p2
p2 = p2!!.next
}
v2 == null -> {
head!!.next = p1
p1 = p1!!.next
}
v1 < v2 -> {
head!!.next = p1
p1 = p1!!.next
}
else -> {
head!!.next = p2
p2 = p2!!.next
}
}
head = head!!.next
}
return dummy.next
}
}
// Same as My1 but this runs on leetcode, because it doesn't use `break` in a while loop which the version of kotlin used
// in leetcode does not support
private class My1_ : Leetcode_21 {
override fun mergeTwoLists(list1: ListNode?, list2: ListNode?): ListNode? {
if (list1 == null) return list2
if (list2 == null) return list1
var p1: ListNode? = list1
var p2: ListNode? = list2
val dummy = ListNode(-1)
var head: ListNode? = dummy
while (p1?.`val` != null || p2?.`val` != null) {
val v1 = p1?.`val`
val v2 = p2?.`val`
when {
v1 == null -> {
head!!.next = p2
p2 = p2!!.next
}
v2 == null -> {
head!!.next = p1
p1 = p1!!.next
}
v1 < v2 -> {
head!!.next = p1
p1 = p1!!.next
}
else -> {
head!!.next = p2
p2 = p2!!.next
}
}
head = head!!.next
}
return dummy.next
}
}
// sample 140 ms submission
private class Sample1 : Leetcode_21 {
override fun mergeTwoLists(list1: ListNode?, list2: ListNode?): ListNode? {
if (list1 == null) return list2
if (list2 == null) return list1
if (list1.`val` < list2.`val`) {
list1.next = mergeTwoLists(list1.next, list2)
return list1
} else {
list2.next = mergeTwoLists(list1, list2.next)
return list2
}
}
}
}
private interface Leetcode_141 {
fun hasCycle(head: ListNode?): Boolean
companion object {
fun test() {
val test1 = cycledNodesOf(intArrayOf(3,2,0,-4), 1)
val test2 = cycledNodesOf(intArrayOf(1,2), 0)
val test3 = cycledNodesOf(intArrayOf(1), -1)
listOf(
My1()::hasCycle,
Sample1()::hasCycle,
).runTimedTests {
assert(invoke(test1))
assert(invoke(test2))
assert(!invoke(test3))
}
}
}
// Runtime: 310 ms, faster than 55.06% of Kotlin online submissions for Linked List Cycle.
// Memory Usage: 44.3 MB, less than 16.67% of Kotlin online submissions for Linked List Cycle.
// https://leetcode.com/problems/linked-list-cycle/submissions/
private class My1 : Leetcode_141 {
override fun hasCycle(head: ListNode?): Boolean {
var slow: ListNode? = head
var fast: ListNode? = head
while (slow?.next != null) {
slow = slow.next
fast = fast?.next?.next ?: return false
if (slow == fast) return true
}
return false
}
}
// sample 160 ms submission
private class Sample1 : Leetcode_141 {
override fun hasCycle(head: ListNode?): Boolean {
var fast = head
var slow = head
while (fast != null) {
fast = fast?.next?.next
slow = slow?.next
if (fast != null && fast === slow) return true
}
return false
}
}
}
private interface Leetcode_142 {
fun detectCycle(head: ListNode?): ListNode?
companion object {
fun test() {
val test1 = cycledNodesOf(intArrayOf(3,2,0,-4), 1)
val test2 = cycledNodesOf(intArrayOf(1,2), 0)
val test3 = cycledNodesOf(intArrayOf(1), -1)
listOf(
Sample1()::detectCycle,
).runTimedTests {
invoke(test1).assertThat {
this == test1[1]
}
invoke(test2).assertThat {
this == test2[0] && this == test2
}
invoke(test3).assertThat {
this == null
}
}
}
}
// https://labuladong.github.io/article/?qno=142
private class Sample1 : Leetcode_142 {
override fun detectCycle(head: ListNode?): ListNode? {
if (head == null) return null
if (head.next == head) return head
var slow: ListNode? = head
var fast: ListNode? = head
while (slow != null && fast != null) {
slow = slow.next
fast = fast.next?.next
if (slow == fast) break
}
// 上面的代码类似 hasCycle 函数
if (fast == null || fast?.next == null) {
// fast 遇到空指针说明没有环
return null
}
// 重新指向头结点
slow = head
// 快慢指针同步前进,相交点就是环起点
while (slow != fast) {
fast = fast?.next;
slow = slow?.next;
}
return slow
}
}
}