mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-13 15:56:05 +00:00
build
This commit is contained in:
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user