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