This commit is contained in:
krahets
2024-04-03 21:48:54 +08:00
parent d5a7899137
commit 0a9daa8b9f
11 changed files with 685 additions and 97 deletions

View File

@@ -314,7 +314,7 @@ comments: true
### 随机访问元素 ###
def random_access(nums)
# 在区间 [0, nums.length) 中随机抽取一个数字
random_index = Random.rand 0...(nums.length - 1)
random_index = Random.rand(0...nums.length)
# 获取并返回随机元素
nums[random_index]

View File

@@ -427,11 +427,11 @@ comments: true
```ruby title="linked_list.rb"
# 初始化链表 1 -> 3 -> 2 -> 5 -> 4
# 初始化各个节点
n0 = ListNode.new 1
n1 = ListNode.new 3
n2 = ListNode.new 2
n3 = ListNode.new 5
n4 = ListNode.new 4
n0 = ListNode.new(1)
n1 = ListNode.new(3)
n2 = ListNode.new(2)
n3 = ListNode.new(5)
n4 = ListNode.new(4)
# 构建节点之间的引用
n0.next = n1
n1.next = n2

View File

@@ -551,10 +551,10 @@ comments: true
nums << 4
# 在中间插入元素
nums.insert 3, 6 # 在索引 3 处插入数字 6
nums.insert(3, 6) # 在索引 3 处插入数字 6
# 删除元素
nums.delete_at 3 # 删除索引 3 处的元素
nums.delete_at(3) # 删除索引 3 处的元素
```
=== "Zig"
@@ -2288,7 +2288,7 @@ comments: true
@capacity = 10
@size = 0
@extend_ratio = 2
@arr = Array.new capacity
@arr = Array.new(capacity)
end
### 访问元素 ###
@@ -2360,9 +2360,9 @@ comments: true
def to_array
sz = size
# 仅转换有效长度范围内的列表元素
arr = Array.new sz
arr = Array.new(sz)
for i in 0...sz
arr[i] = get i
arr[i] = get(i)
end
arr
end