mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-04 03:34:21 +00:00
build
This commit is contained in:
@@ -293,6 +293,19 @@ Accessing elements in an array is highly efficient, allowing us to randomly acce
|
||||
}
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="array.rb"
|
||||
### 随机访问元素 ###
|
||||
def random_access(nums)
|
||||
# 在区间 [0, nums.length) 中随机抽取一个数字
|
||||
random_index = Random.rand 0...(nums.length - 1)
|
||||
|
||||
# 获取并返回随机元素
|
||||
nums[random_index]
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="array.zig"
|
||||
@@ -487,6 +500,21 @@ It's important to note that due to the fixed length of an array, inserting an el
|
||||
}
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="array.rb"
|
||||
### 在数组的索引 index 处插入元素 num ###
|
||||
def insert(nums, num, index)
|
||||
# 把索引 index 以及之后的所有元素向后移动一位
|
||||
for i in (nums.length - 1).downto(index + 1)
|
||||
nums[i] = nums[i - 1]
|
||||
end
|
||||
|
||||
# 将 num 赋给 index 处的元素
|
||||
nums[index] = num
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="array.zig"
|
||||
@@ -660,6 +688,18 @@ Please note that after deletion, the former last element becomes "meaningless,"
|
||||
}
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="array.rb"
|
||||
### 删除索引 index 处的元素 ###
|
||||
def remove(nums, index)
|
||||
# 把索引 index 之后的所有元素向前移动一位
|
||||
for i in index...nums.length
|
||||
nums[i] = nums[i + 1] || 0
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="array.zig"
|
||||
@@ -900,6 +940,25 @@ In most programming languages, we can traverse an array either by using indices
|
||||
}
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="array.rb"
|
||||
### 遍历数组 ###
|
||||
def traverse(nums)
|
||||
count = 0
|
||||
|
||||
# 通过索引遍历数组
|
||||
for i in 0...nums.length
|
||||
count += nums[i]
|
||||
end
|
||||
|
||||
# 直接遍历数组元素
|
||||
for num in nums
|
||||
count += num
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="array.zig"
|
||||
@@ -1087,6 +1146,19 @@ Because arrays are linear data structures, this operation is commonly referred t
|
||||
}
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="array.rb"
|
||||
### 在数组中查找指定元素 ###
|
||||
def find(nums, target)
|
||||
for i in 0...nums.length
|
||||
return i if nums[i] == target
|
||||
end
|
||||
|
||||
-1
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="array.zig"
|
||||
@@ -1310,6 +1382,26 @@ To expand an array, it's necessary to create a larger array and then copy the e
|
||||
}
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="array.rb"
|
||||
### 扩展数组长度 ###
|
||||
# 请注意,Ruby 的 Array 是动态数组,可以直接扩展
|
||||
# 为了方便学习,本函数将 Array 看作长度不可变的数组
|
||||
def extend(nums, enlarge)
|
||||
# 初始化一个扩展长度后的数组
|
||||
res = Array.new(nums.length + enlarge, 0)
|
||||
|
||||
# 将原数组中的所有元素复制到新数组
|
||||
for i in 0...nums.length
|
||||
res[i] = nums[i]
|
||||
end
|
||||
|
||||
# 返回扩展后的新数组
|
||||
res
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="array.zig"
|
||||
|
||||
Reference in New Issue
Block a user