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

View File

@ -111,6 +111,12 @@ comments: true
int nums[5] = { 1, 3, 2, 5, 4 };
```
=== "Kotlin"
```kotlin title="array.kt"
```
=== "Zig"
```zig title="array.zig"
@ -279,6 +285,19 @@ comments: true
}
```
=== "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"
@ -459,6 +478,20 @@ comments: true
}
```
=== "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"
@ -620,6 +653,18 @@ comments: true
}
```
=== "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"
@ -843,6 +888,23 @@ comments: true
}
```
=== "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"
@ -1018,6 +1080,18 @@ comments: true
}
```
=== "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"
@ -1225,6 +1299,22 @@ comments: true
}
```
=== "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"

View File

@ -165,6 +165,12 @@ comments: true
}
```
=== "Kotlin"
```kotlin title=""
```
=== "Zig"
```zig title=""
@ -379,6 +385,12 @@ comments: true
n3->next = n4;
```
=== "Kotlin"
```kotlin title="linked_list.kt"
```
=== "Zig"
```zig title="linked_list.zig"
@ -534,6 +546,17 @@ comments: true
}
```
=== "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"
@ -723,6 +746,17 @@ comments: true
}
```
=== "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"
@ -903,6 +937,19 @@ comments: true
}
```
=== "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"
@ -1106,6 +1153,22 @@ comments: true
}
```
=== "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"
@ -1323,6 +1386,12 @@ comments: true
}
```
=== "Kotlin"
```kotlin title=""
```
=== "Zig"
```zig title=""

View File

@ -130,6 +130,12 @@ comments: true
// C 未提供内置动态数组
```
=== "Kotlin"
```kotlin title="list.kt"
```
=== "Zig"
```zig title="list.zig"
@ -253,6 +259,12 @@ comments: true
// C 未提供内置动态数组
```
=== "Kotlin"
```kotlin title="list.kt"
```
=== "Zig"
```zig title="list.zig"
@ -478,6 +490,12 @@ comments: true
// C 未提供内置动态数组
```
=== "Kotlin"
```kotlin title="list.kt"
```
=== "Zig"
```zig title="list.zig"
@ -669,6 +687,12 @@ comments: true
// C 未提供内置动态数组
```
=== "Kotlin"
```kotlin title="list.kt"
```
=== "Zig"
```zig title="list.zig"
@ -782,6 +806,12 @@ comments: true
// C 未提供内置动态数组
```
=== "Kotlin"
```kotlin title="list.kt"
```
=== "Zig"
```zig title="list.zig"
@ -877,6 +907,12 @@ comments: true
// C 未提供内置动态数组
```
=== "Kotlin"
```kotlin title="list.kt"
```
=== "Zig"
```zig title="list.zig"
@ -2035,6 +2071,106 @@ comments: true
}
```
=== "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"