mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 10:56:29 +08:00
Fix inconsistent comments Ruby - chapter array and linked list (#1202)
* fix: inconsistent comments Ruby - chapter array and linked list * fix: better Ruby code & comments
This commit is contained in:
parent
57bdfd6284
commit
5ce088de52
3 changed files with 15 additions and 15 deletions
|
@ -9,8 +9,8 @@ class ListNode
|
||||||
attr_accessor :val # 节点值
|
attr_accessor :val # 节点值
|
||||||
attr_accessor :next # 指向下一节点的引用
|
attr_accessor :next # 指向下一节点的引用
|
||||||
|
|
||||||
def initialize(val=nil, next_node=nil)
|
def initialize(val=0, next_node=nil)
|
||||||
@val = val || 0
|
@val = val
|
||||||
@next = next_node
|
@next = next_node
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -23,7 +23,7 @@ def arr_to_linked_list(arr)
|
||||||
current.next = ListNode.new arr[i]
|
current.next = ListNode.new arr[i]
|
||||||
current = current.next
|
current = current.next
|
||||||
end
|
end
|
||||||
|
|
||||||
head
|
head
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -178,8 +178,8 @@
|
||||||
attr_accessor :val # 节点值
|
attr_accessor :val # 节点值
|
||||||
attr_accessor :next # 指向下一节点的引用
|
attr_accessor :next # 指向下一节点的引用
|
||||||
|
|
||||||
def initialize(val=nil, next_node=nil)
|
def initialize(val=0, next_node=nil)
|
||||||
@val = val || 0
|
@val = val
|
||||||
@next = next_node
|
@next = next_node
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -709,8 +709,8 @@
|
||||||
attr_accessor :next # 指向后继节点的引用
|
attr_accessor :next # 指向后继节点的引用
|
||||||
attr_accessor :prev # 指向前驱节点的引用
|
attr_accessor :prev # 指向前驱节点的引用
|
||||||
|
|
||||||
def initialize(val=nil, next_node=nil, prev_node=nil)
|
def initialize(val=0, next_node=nil, prev_node=nil)
|
||||||
@val = val || 0
|
@val = val
|
||||||
@next = next_node
|
@next = next_node
|
||||||
@prev = prev_node
|
@prev = prev_node
|
||||||
end
|
end
|
||||||
|
|
|
@ -282,9 +282,9 @@
|
||||||
|
|
||||||
```ruby title="list.rb"
|
```ruby title="list.rb"
|
||||||
# 访问元素
|
# 访问元素
|
||||||
num = nums[1]
|
num = nums[1] # 访问索引 1 处的元素
|
||||||
# 更新元素
|
# 更新元素
|
||||||
nums[1] = 0
|
nums[1] = 0 # 将索引 1 处的元素更新为 0
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "Zig"
|
=== "Zig"
|
||||||
|
@ -294,7 +294,7 @@
|
||||||
var num = nums.items[1]; // 访问索引 1 处的元素
|
var num = nums.items[1]; // 访问索引 1 处的元素
|
||||||
|
|
||||||
// 更新元素
|
// 更新元素
|
||||||
nums.items[1] = 0; // 将索引 1 处的元素更新为 0
|
nums.items[1] = 0; // 将索引 1 处的元素更新为 0
|
||||||
```
|
```
|
||||||
|
|
||||||
??? pythontutor "可视化运行"
|
??? pythontutor "可视化运行"
|
||||||
|
@ -545,10 +545,10 @@
|
||||||
nums << 4
|
nums << 4
|
||||||
|
|
||||||
# 在中间插入元素
|
# 在中间插入元素
|
||||||
nums.insert 3, 6
|
nums.insert 3, 6 # 在索引 3 处插入数字 6
|
||||||
|
|
||||||
# 删除元素
|
# 删除元素
|
||||||
nums.delete_at 3
|
nums.delete_at 3 # 删除索引 3 处的元素
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "Zig"
|
=== "Zig"
|
||||||
|
@ -711,7 +711,7 @@
|
||||||
for (var i = 0; i < nums.length; i++) {
|
for (var i = 0; i < nums.length; i++) {
|
||||||
count += nums[i];
|
count += nums[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 直接遍历列表元素 */
|
/* 直接遍历列表元素 */
|
||||||
count = 0;
|
count = 0;
|
||||||
for (var num in nums) {
|
for (var num in nums) {
|
||||||
|
@ -963,7 +963,7 @@
|
||||||
=== "JS"
|
=== "JS"
|
||||||
|
|
||||||
```javascript title="list.js"
|
```javascript title="list.js"
|
||||||
/* 排序列表 */
|
/* 排序列表 */
|
||||||
nums.sort((a, b) => a - b); // 排序后,列表元素从小到大排列
|
nums.sort((a, b) => a - b); // 排序后,列表元素从小到大排列
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1005,7 +1005,7 @@
|
||||||
|
|
||||||
```ruby title="list.rb"
|
```ruby title="list.rb"
|
||||||
# 排序列表
|
# 排序列表
|
||||||
nums = nums.sort { |a, b| a <=> b }
|
nums = nums.sort { |a, b| a <=> b } # 排序后,列表元素从小到大排列
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "Zig"
|
=== "Zig"
|
||||||
|
|
Loading…
Reference in a new issue