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

View File

@ -114,7 +114,17 @@ comments: true
=== "Kotlin"
```kotlin title="array.kt"
/* 初始化数组 */
var arr = IntArray(5) // { 0, 0, 0, 0, 0 }
var nums = intArrayOf(1, 3, 2, 5, 4)
```
=== "Ruby"
```ruby title="array.rb"
# 初始化数组
arr = Array.new(5, 0)
nums = [1, 3, 2, 5, 4]
```
=== "Zig"
@ -298,6 +308,19 @@ comments: true
}
```
=== "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"
@ -492,6 +515,21 @@ comments: true
}
```
=== "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"
@ -665,6 +703,18 @@ comments: true
}
```
=== "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"
@ -905,6 +955,25 @@ comments: true
}
```
=== "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"
@ -1092,6 +1161,19 @@ comments: true
}
```
=== "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"
@ -1315,6 +1397,26 @@ comments: true
}
```
=== "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"

View File

@ -168,7 +168,27 @@ comments: true
=== "Kotlin"
```kotlin title=""
/* 链表节点类 */
// 构造方法
class ListNode(x: Int) {
val _val: Int = x // 节点值
val next: ListNode? = null // 指向下一个节点的引用
}
```
=== "Ruby"
```ruby title=""
# 链表节点类
class ListNode
attr_accessor :val # 节点值
attr_accessor :next # 指向下一节点的引用
def initialize(val=nil, next_node=nil)
@val = val || 0
@next = next_node
end
end
```
=== "Zig"
@ -388,7 +408,35 @@ comments: true
=== "Kotlin"
```kotlin title="linked_list.kt"
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
// 初始化各个节点
val n0 = ListNode(1)
val n1 = ListNode(3)
val n2 = ListNode(2)
val n3 = ListNode(5)
val n4 = ListNode(4)
// 构建节点之间的引用
n0.next = n1;
n1.next = n2;
n2.next = n3;
n3.next = n4;
```
=== "Ruby"
```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.next = n1
n1.next = n2
n2.next = n3
n3.next = n4
```
=== "Zig"
@ -557,6 +605,18 @@ comments: true
}
```
=== "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"
@ -757,6 +817,20 @@ comments: true
}
```
=== "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"
@ -950,6 +1024,20 @@ comments: true
}
```
=== "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"
@ -1169,6 +1257,22 @@ comments: true
}
```
=== "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"
@ -1389,7 +1493,30 @@ comments: true
=== "Kotlin"
```kotlin title=""
/* 双向链表节点类 */
// 构造方法
class ListNode(x: Int) {
val _val: Int = x // 节点值
val next: ListNode? = null // 指向后继节点的引用
val prev: ListNode? = null // 指向前驱节点的引用
}
```
=== "Ruby"
```ruby title=""
# 双向链表节点类
class ListNode
attr_accessor :val # 节点值
attr_accessor :next # 指向后继节点的引用
attr_accessor :prev # 指向前驱节点的引用
def initialize(val=nil, next_node=nil, prev_node=nil)
@val = val || 0
@next = next_node
@prev = prev_node
end
end
```
=== "Zig"

View File

@ -133,7 +133,22 @@ comments: true
=== "Kotlin"
```kotlin title="list.kt"
/* 初始化列表 */
// 无初始值
var nums1 = listOf<Int>()
// 有初始值
var numbers = arrayOf(1, 3, 2, 5, 4)
var nums = numbers.toMutableList()
```
=== "Ruby"
```ruby title="list.rb"
# 初始化列表
# 无初始值
nums1 = []
# 有初始值
nums = [1, 3, 2, 5, 4]
```
=== "Zig"
@ -262,7 +277,19 @@ comments: true
=== "Kotlin"
```kotlin title="list.kt"
/* 访问元素 */
val num = nums[1] // 访问索引 1 处的元素
/* 更新元素 */
nums[1] = 0 // 将索引 1 处的元素更新为 0
```
=== "Ruby"
```ruby title="list.rb"
# 访问元素
num = nums[1]
# 更新元素
nums[1] = 0
```
=== "Zig"
@ -493,7 +520,41 @@ comments: true
=== "Kotlin"
```kotlin title="list.kt"
/* 清空列表 */
nums.clear();
/* 在尾部添加元素 */
nums.add(1);
nums.add(3);
nums.add(2);
nums.add(5);
nums.add(4);
/* 在中间插入元素 */
nums.add(3, 6); // 在索引 3 处插入数字 6
/* 删除元素 */
nums.remove(3); // 删除索引 3 处的元素
```
=== "Ruby"
```ruby title="list.rb"
# 清空列表
nums.clear
# 在尾部添加元素
nums << 1
nums << 3
nums << 2
nums << 5
nums << 4
# 在中间插入元素
nums.insert 3, 6
# 删除元素
nums.delete_at 3
```
=== "Zig"
@ -690,7 +751,32 @@ comments: true
=== "Kotlin"
```kotlin title="list.kt"
/* 通过索引遍历列表 */
var count = 0
for (i in nums.indices) {
count += nums[i]
}
/* 直接遍历列表元素 */
for (num in nums) {
count += num
}
```
=== "Ruby"
```ruby title="list.rb"
# 通过索引遍历列表
count = 0
for i in 0...nums.length
count += nums[i]
end
# 直接遍历列表元素
count = 0
for num in nums
count += num
end
```
=== "Zig"
@ -809,7 +895,17 @@ comments: true
=== "Kotlin"
```kotlin title="list.kt"
/* 拼接两个列表 */
val nums1 = intArrayOf(6, 8, 7, 10, 9).toMutableList()
nums.addAll(nums1) // 将列表 nums1 拼接到 nums 之后
```
=== "Ruby"
```ruby title="list.rb"
# 拼接两个列表
nums1 = [6, 8, 7, 10, 9]
nums += nums1
```
=== "Zig"
@ -910,7 +1006,15 @@ comments: true
=== "Kotlin"
```kotlin title="list.kt"
/* 排序列表 */
nums.sort() // 排序后,列表元素从小到大排列
```
=== "Ruby"
```ruby title="list.rb"
# 排序列表
nums = nums.sort { |a, b| a <=> b }
```
=== "Zig"
@ -2171,6 +2275,100 @@ comments: true
}
```
=== "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"