This commit is contained in:
krahets
2024-03-25 22:43:12 +08:00
parent 22017aa8e5
commit 87af663929
70 changed files with 7428 additions and 32 deletions
@@ -111,6 +111,12 @@ Arrays can be initialized in two ways depending on the needs: either without ini
int nums[5] = { 1, 3, 2, 5, 4 };
```
=== "Kotlin"
```kotlin title="array.kt"
```
=== "Zig"
```zig title="array.zig"
@@ -274,6 +280,19 @@ Accessing elements in an array is highly efficient, allowing us to randomly acce
}
```
=== "Kotlin"
```kotlin title="array.kt"
/* 随机访问元素 */
fun randomAccess(nums: IntArray): Int {
// 在区间 [0, nums.size) 中随机抽取一个数字
val randomIndex = ThreadLocalRandom.current().nextInt(0, nums.size)
// 获取并返回随机元素
val randomNum = nums[randomIndex]
return randomNum
}
```
=== "Zig"
```zig title="array.zig"
@@ -454,6 +473,20 @@ It's important to note that due to the fixed length of an array, inserting an el
}
```
=== "Kotlin"
```kotlin title="array.kt"
/* 在数组的索引 index 处插入元素 num */
fun insert(nums: IntArray, num: Int, index: Int) {
// 把索引 index 以及之后的所有元素向后移动一位
for (i in nums.size - 1 downTo index + 1) {
nums[i] = nums[i - 1]
}
// 将 num 赋给 index 处的元素
nums[index] = num
}
```
=== "Zig"
```zig title="array.zig"
@@ -615,6 +648,18 @@ Please note that after deletion, the former last element becomes "meaningless,"
}
```
=== "Kotlin"
```kotlin title="array.kt"
/* 删除索引 index 处的元素 */
fun remove(nums: IntArray, index: Int) {
// 把索引 index 之后的所有元素向前移动一位
for (i in index..<nums.size - 1) {
nums[i] = nums[i + 1]
}
}
```
=== "Zig"
```zig title="array.zig"
@@ -838,6 +883,23 @@ In most programming languages, we can traverse an array either by using indices
}
```
=== "Kotlin"
```kotlin title="array.kt"
/* 遍历数组 */
fun traverse(nums: IntArray) {
var count = 0
// 通过索引遍历数组
for (i in nums.indices) {
count += nums[i]
}
// 直接遍历数组元素
for (j: Int in nums) {
count += j
}
}
```
=== "Zig"
```zig title="array.zig"
@@ -1013,6 +1075,18 @@ Because arrays are linear data structures, this operation is commonly referred t
}
```
=== "Kotlin"
```kotlin title="array.kt"
/* 在数组中查找指定元素 */
fun find(nums: IntArray, target: Int): Int {
for (i in nums.indices) {
if (nums[i] == target) return i
}
return -1
}
```
=== "Zig"
```zig title="array.zig"
@@ -1220,6 +1294,22 @@ To expand an array, it's necessary to create a larger array and then copy the e
}
```
=== "Kotlin"
```kotlin title="array.kt"
/* 扩展数组长度 */
fun extend(nums: IntArray, enlarge: Int): IntArray {
// 初始化一个扩展长度后的数组
val res = IntArray(nums.size + enlarge)
// 将原数组中的所有元素复制到新数组
for (i in nums.indices) {
res[i] = nums[i]
}
// 返回扩展后的新数组
return res
}
```
=== "Zig"
```zig title="array.zig"
@@ -165,6 +165,12 @@ As the code below illustrates, a `ListNode` in a linked list, besides holding a
}
```
=== "Kotlin"
```kotlin title=""
```
=== "Zig"
```zig title=""
@@ -379,6 +385,12 @@ Constructing a linked list is a two-step process: first, initializing each node
n3->next = n4;
```
=== "Kotlin"
```kotlin title="linked_list.kt"
```
=== "Zig"
```zig title="linked_list.zig"
@@ -529,6 +541,17 @@ By comparison, inserting an element into an array has a time complexity of $O(n)
}
```
=== "Kotlin"
```kotlin title="linked_list.kt"
/* 在链表的节点 n0 之后插入节点p */
fun insert(n0: ListNode?, p: ListNode?) {
val n1 = n0?.next
p?.next = n1
n0?.next = p
}
```
=== "Zig"
```zig title="linked_list.zig"
@@ -718,6 +741,17 @@ It's important to note that even though node `P` continues to point to `n1` afte
}
```
=== "Kotlin"
```kotlin title="linked_list.kt"
/* 删除链表的节点 n0 之后的首个节点 */
fun remove(n0: ListNode?) {
val p = n0?.next
val n1 = p?.next
n0?.next = n1
}
```
=== "Zig"
```zig title="linked_list.zig"
@@ -898,6 +932,19 @@ It's important to note that even though node `P` continues to point to `n1` afte
}
```
=== "Kotlin"
```kotlin title="linked_list.kt"
/* 访问链表中索引为 index 的节点 */
fun access(head: ListNode?, index: Int): ListNode? {
var h = head
for (i in 0..<index) {
h = h?.next
}
return h
}
```
=== "Zig"
```zig title="linked_list.zig"
@@ -1101,6 +1148,22 @@ Traverse the linked list to locate a node whose value matches `target`, and then
}
```
=== "Kotlin"
```kotlin title="linked_list.kt"
/* 在链表中查找值为 target 的首个节点 */
fun find(head: ListNode?, target: Int): Int {
var index = 0
var h = head
while (h != null) {
if (h.value == target) return index
h = h.next
index++
}
return -1
}
```
=== "Zig"
```zig title="linked_list.zig"
@@ -1318,6 +1381,12 @@ As shown in the figure, there are three common types of linked lists.
}
```
=== "Kotlin"
```kotlin title=""
```
=== "Zig"
```zig title=""
@@ -130,6 +130,12 @@ We typically use two initialization methods: "without initial values" and "with
// C does not provide built-in dynamic arrays
```
=== "Kotlin"
```kotlin title="list.kt"
```
=== "Zig"
```zig title="list.zig"
@@ -248,6 +254,12 @@ Lists are essentially arrays, thus they can access and update elements in $O(1)$
// C does not provide built-in dynamic arrays
```
=== "Kotlin"
```kotlin title="list.kt"
```
=== "Zig"
```zig title="list.zig"
@@ -468,6 +480,12 @@ Compared to arrays, lists offer more flexibility in adding and removing elements
// C does not provide built-in dynamic arrays
```
=== "Kotlin"
```kotlin title="list.kt"
```
=== "Zig"
```zig title="list.zig"
@@ -654,6 +672,12 @@ Similar to arrays, lists can be iterated either by using indices or by directly
// C does not provide built-in dynamic arrays
```
=== "Kotlin"
```kotlin title="list.kt"
```
=== "Zig"
```zig title="list.zig"
@@ -762,6 +786,12 @@ Given a new list `nums1`, we can append it to the end of the original list.
// C does not provide built-in dynamic arrays
```
=== "Kotlin"
```kotlin title="list.kt"
```
=== "Zig"
```zig title="list.zig"
@@ -852,6 +882,12 @@ Once the list is sorted, we can employ algorithms commonly used in array-related
// C does not provide built-in dynamic arrays
```
=== "Kotlin"
```kotlin title="list.kt"
```
=== "Zig"
```zig title="list.zig"
@@ -2005,6 +2041,106 @@ To enhance our understanding of how lists work, we will attempt to implement a s
}
```
=== "Kotlin"
```kotlin title="my_list.kt"
/* 列表类 */
class MyList {
private var arr: IntArray = intArrayOf() // 数组(存储列表元素)
private var capacity = 10 // 列表容量
private var size = 0 // 列表长度(当前元素数量)
private var extendRatio = 2 // 每次列表扩容的倍数
/* 构造函数 */
init {
arr = IntArray(capacity)
}
/* 获取列表长度(当前元素数量) */
fun size(): Int {
return size
}
/* 获取列表容量 */
fun capacity(): Int {
return capacity
}
/* 访问元素 */
fun get(index: Int): Int {
// 索引如果越界,则抛出异常,下同
if (index < 0 || index >= size)
throw IndexOutOfBoundsException()
return arr[index]
}
/* 更新元素 */
fun set(index: Int, num: Int) {
if (index < 0 || index >= size)
throw IndexOutOfBoundsException("索引越界")
arr[index] = num
}
/* 在尾部添加元素 */
fun add(num: Int) {
// 元素数量超出容量时,触发扩容机制
if (size == capacity())
extendCapacity()
arr[size] = num
// 更新元素数量
size++
}
/* 在中间插入元素 */
fun insert(index: Int, num: Int) {
if (index < 0 || index >= size)
throw IndexOutOfBoundsException("索引越界")
// 元素数量超出容量时,触发扩容机制
if (size == capacity())
extendCapacity()
// 将索引 index 以及之后的元素都向后移动一位
for (j in size - 1 downTo index)
arr[j + 1] = arr[j]
arr[index] = num
// 更新元素数量
size++
}
/* 删除元素 */
fun remove(index: Int): Int {
if (index < 0 || index >= size)
throw IndexOutOfBoundsException("索引越界")
val num: Int = arr[index]
// 将将索引 index 之后的元素都向前移动一位
for (j in index..<size - 1)
arr[j] = arr[j + 1]
// 更新元素数量
size--
// 返回被删除的元素
return num
}
/* 列表扩容 */
fun extendCapacity() {
// 新建一个长度为原数组 extendRatio 倍的新数组,并将原数组复制到新数组
arr = arr.copyOf(capacity() * extendRatio)
// 更新列表容量
capacity = arr.size
}
/* 将列表转换为数组 */
fun toArray(): IntArray {
val size = size()
// 仅转换有效长度范围内的列表元素
val arr = IntArray(size)
for (i in 0..<size) {
arr[i] = get(i)
}
return arr
}
}
```
=== "Zig"
```zig title="my_list.zig"