This commit is contained in:
krahets
2024-03-31 03:53:04 +08:00
parent 87af663929
commit c23e576da4
68 changed files with 2139 additions and 22 deletions
@@ -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"
@@ -552,6 +552,18 @@ By comparison, inserting an element into an array has a time complexity of $O(n)
}
```
=== "Ruby"
```ruby title="linked_list.rb"
### 在链表的节点 n0 之后插入节点 _p ###
# Ruby 的 `p` 是一个内置函数, `P` 是一个常量,所以可以使用 `_p` 代替
def insert(n0, _p)
n1 = n0.next
_p.next = n1
n0.next = _p
end
```
=== "Zig"
```zig title="linked_list.zig"
@@ -752,6 +764,20 @@ It's important to note that even though node `P` continues to point to `n1` afte
}
```
=== "Ruby"
```ruby title="linked_list.rb"
### 删除链表的节点 n0 之后的首个节点 ###
def remove(n0)
return if n0.next.nil?
# n0 -> remove_node -> n1
remove_node = n0.next
n1 = remove_node.next
n0.next = n1
end
```
=== "Zig"
```zig title="linked_list.zig"
@@ -945,6 +971,20 @@ It's important to note that even though node `P` continues to point to `n1` afte
}
```
=== "Ruby"
```ruby title="linked_list.rb"
### 访问链表中索引为 index 的节点 ###
def access(head, index)
for i in 0...index
return nil if head.nil?
head = head.next
end
head
end
```
=== "Zig"
```zig title="linked_list.zig"
@@ -1164,6 +1204,22 @@ Traverse the linked list to locate a node whose value matches `target`, and then
}
```
=== "Ruby"
```ruby title="linked_list.rb"
### 在链表中查找值为 target 的首个节点 ###
def find(head, target)
index = 0
while head
return index if head.val == target
head = head.next
index += 1
end
-1
end
```
=== "Zig"
```zig title="linked_list.zig"
@@ -2141,6 +2141,100 @@ To enhance our understanding of how lists work, we will attempt to implement a s
}
```
=== "Ruby"
```ruby title="my_list.rb"
### 列表类 ###
class MyList
attr_reader :size # 获取列表长度(当前元素数量)
attr_reader :capacity # 获取列表容量
### 构造方法 ###
def initialize
@capacity = 10
@size = 0
@extend_ratio = 2
@arr = Array.new capacity
end
### 访问元素 ###
def get(index)
# 索引如果越界,则抛出异常,下同
raise IndexError, "索引越界" if index < 0 || index >= size
@arr[index]
end
### 访问元素 ###
def set(index, num)
raise IndexError, "索引越界" if index < 0 || index >= size
@arr[index] = num
end
### 在尾部添加元素 ###
def add(num)
# 元素数量超出容量时,触发扩容机制
extend_capacity if size == capacity
@arr[size] = num
# 更新元素数量
@size += 1
end
### 在中间插入元素 ###
def insert(index, num)
raise IndexError, "索引越界" if index < 0 || index >= size
# 元素数量超出容量时,触发扩容机制
extend_capacity if size == capacity
# 将索引 index 以及之后的元素都向后移动一位
for j in (size - 1).downto(index)
@arr[j + 1] = @arr[j]
end
@arr[index] = num
# 更新元素数量
@size += 1
end
### 删除元素 ###
def remove(index)
raise IndexError, "索引越界" if index < 0 || index >= size
num = @arr[index]
# 将将索引 index 之后的元素都向前移动一位
for j in index...size
@arr[j] = @arr[j + 1]
end
# 更新元素数量
@size -= 1
# 返回被删除的元素
num
end
### 列表扩容 ###
def extend_capacity
# 新建一个长度为原数组 extend_ratio 倍的新数组,并将原数组复制到新数组
arr = @arr.dup + Array.new(capacity * (@extend_ratio - 1))
# 更新列表容量
@capacity = arr.length
end
### 将列表转换为数组 ###
def to_array
sz = size
# 仅转换有效长度范围内的列表元素
arr = Array.new sz
for i in 0...sz
arr[i] = get i
end
arr
end
end
```
=== "Zig"
```zig title="my_list.zig"