diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index 2e5a31c52..58ee8b1d1 100755 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -113,14 +113,6 @@ var nums = intArrayOf(1, 3, 2, 5, 4) ``` -=== "Zig" - - ```zig title="array.zig" - // 初始化数组 - var arr = [_]i32{0} ** 5; // { 0, 0, 0, 0, 0 } - var nums = [_]i32{ 1, 3, 2, 5, 4 }; - ``` - === "Ruby" ```ruby title="array.rb" @@ -129,6 +121,14 @@ nums = [1, 3, 2, 5, 4] ``` +=== "Zig" + + ```zig title="array.zig" + // 初始化数组 + var arr = [_]i32{0} ** 5; // { 0, 0, 0, 0, 0 } + var nums = [_]i32{ 1, 3, 2, 5, 4 }; + ``` + ??? pythontutor "可视化运行" https://pythontutor.com/render.html#code=%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E6%95%B0%E7%BB%84%0Aarr%20%3D%20%5B0%5D%20*%205%20%20%23%20%5B%200,%200,%200,%200,%200%20%5D%0Anums%20%3D%20%5B1,%203,%202,%205,%204%5D&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index 9c9b09838..66e048b28 100755 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -170,6 +170,21 @@ } ``` +=== "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" ```zig title="" @@ -190,21 +205,6 @@ } ``` -=== "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 - ``` - ## 链表常用操作 ### 初始化链表 @@ -416,6 +416,23 @@ 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" ```zig title="linked_list.zig" @@ -433,23 +450,6 @@ 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 - ``` - ??? pythontutor "可视化运行" https://pythontutor.com/render.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%93%BE%E8%A1%A8%201%20-%3E%203%20-%3E%202%20-%3E%205%20-%3E%204%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E4%B8%AA%E8%8A%82%E7%82%B9%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false @@ -700,6 +700,23 @@ } ``` +=== "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" ```zig title="" @@ -722,23 +739,6 @@ } ``` -=== "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 - ``` - ![常见链表种类](linked_list.assets/linkedlist_common_types.png) ## 链表典型应用 diff --git a/docs/chapter_array_and_linkedlist/list.md b/docs/chapter_array_and_linkedlist/list.md index 345bdc99f..bb1ce528b 100755 --- a/docs/chapter_array_and_linkedlist/list.md +++ b/docs/chapter_array_and_linkedlist/list.md @@ -137,15 +137,6 @@ var nums = numbers.toMutableList() ``` -=== "Zig" - - ```zig title="list.zig" - // 初始化列表 - var nums = std.ArrayList(i32).init(std.heap.page_allocator); - defer nums.deinit(); - try nums.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 }); - ``` - === "Ruby" ```ruby title="list.rb" @@ -156,6 +147,15 @@ nums = [1, 3, 2, 5, 4] ``` +=== "Zig" + + ```zig title="list.zig" + // 初始化列表 + var nums = std.ArrayList(i32).init(std.heap.page_allocator); + defer nums.deinit(); + try nums.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 }); + ``` + ??? pythontutor "可视化运行" https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20%23%20%E6%97%A0%E5%88%9D%E5%A7%8B%E5%80%BC%0A%20%20%20%20nums1%20%3D%20%5B%5D%0A%20%20%20%20%23%20%E6%9C%89%E5%88%9D%E5%A7%8B%E5%80%BC%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D&cumulative=false&curInstr=4&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false @@ -278,6 +278,15 @@ nums[1] = 0 // 将索引 1 处的元素更新为 0 ``` +=== "Ruby" + + ```ruby title="list.rb" + # 访问元素 + num = nums[1] + # 更新元素 + nums[1] = 0 + ``` + === "Zig" ```zig title="list.zig" @@ -288,15 +297,6 @@ nums.items[1] = 0; // 将索引 1 处的元素更新为 0 ``` -=== "Ruby" - - ```ruby title="list.rb" - # 访问元素 - num = nums[1] - # 更新元素 - nums[1] = 0 - ``` - ??? pythontutor "可视化运行" https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%0A%20%20%20%20%23%20%E8%AE%BF%E9%97%AE%E5%85%83%E7%B4%A0%0A%20%20%20%20num%20%3D%20nums%5B1%5D%20%20%23%20%E8%AE%BF%E9%97%AE%E7%B4%A2%E5%BC%95%201%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0%0A%0A%20%20%20%20%23%20%E6%9B%B4%E6%96%B0%E5%85%83%E7%B4%A0%0A%20%20%20%20nums%5B1%5D%20%3D%200%20%20%20%20%23%20%E5%B0%86%E7%B4%A2%E5%BC%95%201%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0%E6%9B%B4%E6%96%B0%E4%B8%BA%200&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false @@ -531,26 +531,6 @@ nums.remove(3); // 删除索引 3 处的元素 ``` -=== "Zig" - - ```zig title="list.zig" - // 清空列表 - nums.clearRetainingCapacity(); - - // 在尾部添加元素 - try nums.append(1); - try nums.append(3); - try nums.append(2); - try nums.append(5); - try nums.append(4); - - // 在中间插入元素 - try nums.insert(3, 6); // 在索引 3 处插入数字 6 - - // 删除元素 - _ = nums.orderedRemove(3); // 删除索引 3 处的元素 - ``` - === "Ruby" ```ruby title="list.rb" @@ -571,6 +551,26 @@ nums.delete_at 3 ``` +=== "Zig" + + ```zig title="list.zig" + // 清空列表 + nums.clearRetainingCapacity(); + + // 在尾部添加元素 + try nums.append(1); + try nums.append(3); + try nums.append(2); + try nums.append(5); + try nums.append(4); + + // 在中间插入元素 + try nums.insert(3, 6); // 在索引 3 处插入数字 6 + + // 删除元素 + _ = nums.orderedRemove(3); // 删除索引 3 处的元素 + ``` + ??? pythontutor "可视化运行" https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E6%9C%89%E5%88%9D%E5%A7%8B%E5%80%BC%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%B8%85%E7%A9%BA%E5%88%97%E8%A1%A8%0A%20%20%20%20nums.clear%28%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%9C%A8%E5%B0%BE%E9%83%A8%E6%B7%BB%E5%8A%A0%E5%85%83%E7%B4%A0%0A%20%20%20%20nums.append%281%29%0A%20%20%20%20nums.append%283%29%0A%20%20%20%20nums.append%282%29%0A%20%20%20%20nums.append%285%29%0A%20%20%20%20nums.append%284%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%9C%A8%E4%B8%AD%E9%97%B4%E6%8F%92%E5%85%A5%E5%85%83%E7%B4%A0%0A%20%20%20%20nums.insert%283,%206%29%20%20%23%20%E5%9C%A8%E7%B4%A2%E5%BC%95%203%20%E5%A4%84%E6%8F%92%E5%85%A5%E6%95%B0%E5%AD%97%206%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E5%85%83%E7%B4%A0%0A%20%20%20%20nums.pop%283%29%20%20%20%20%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E7%B4%A2%E5%BC%95%203%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false @@ -756,6 +756,22 @@ } ``` +=== "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" ```zig title="list.zig" @@ -773,22 +789,6 @@ } ``` -=== "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 - ``` - ??? pythontutor "可视化运行" https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E9%80%9A%E8%BF%87%E7%B4%A2%E5%BC%95%E9%81%8D%E5%8E%86%E5%88%97%E8%A1%A8%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20for%20i%20in%20range%28len%28nums%29%29%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20nums%5Bi%5D%0A%0A%20%20%20%20%23%20%E7%9B%B4%E6%8E%A5%E9%81%8D%E5%8E%86%E5%88%97%E8%A1%A8%E5%85%83%E7%B4%A0%0A%20%20%20%20for%20num%20in%20nums%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20num&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false @@ -892,6 +892,14 @@ nums.addAll(nums1) // 将列表 nums1 拼接到 nums 之后 ``` +=== "Ruby" + + ```ruby title="list.rb" + # 拼接两个列表 + nums1 = [6, 8, 7, 10, 9] + nums += nums1 + ``` + === "Zig" ```zig title="list.zig" @@ -902,14 +910,6 @@ try nums.insertSlice(nums.items.len, nums1.items); // 将列表 nums1 拼接到 nums 之后 ``` -=== "Ruby" - - ```ruby title="list.rb" - # 拼接两个列表 - nums1 = [6, 8, 7, 10, 9] - nums += nums1 - ``` - ??? pythontutor "可视化运行" https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%8B%BC%E6%8E%A5%E4%B8%A4%E4%B8%AA%E5%88%97%E8%A1%A8%0A%20%20%20%20nums1%20%3D%20%5B6,%208,%207,%2010,%209%5D%0A%20%20%20%20nums%20%2B%3D%20nums1%20%20%23%20%E5%B0%86%E5%88%97%E8%A1%A8%20nums1%20%E6%8B%BC%E6%8E%A5%E5%88%B0%20nums%20%E4%B9%8B%E5%90%8E&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false @@ -1001,13 +1001,6 @@ nums.sort() // 排序后,列表元素从小到大排列 ``` -=== "Zig" - - ```zig title="list.zig" - // 排序列表 - std.sort.sort(i32, nums.items, {}, comptime std.sort.asc(i32)); - ``` - === "Ruby" ```ruby title="list.rb" @@ -1015,6 +1008,13 @@ nums = nums.sort { |a, b| a <=> b } ``` +=== "Zig" + + ```zig title="list.zig" + // 排序列表 + std.sort.sort(i32, nums.items, {}, comptime std.sort.asc(i32)); + ``` + ??? pythontutor "可视化运行" https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%8E%92%E5%BA%8F%E5%88%97%E8%A1%A8%0A%20%20%20%20nums.sort%28%29%20%20%23%20%E6%8E%92%E5%BA%8F%E5%90%8E%EF%BC%8C%E5%88%97%E8%A1%A8%E5%85%83%E7%B4%A0%E4%BB%8E%E5%B0%8F%E5%88%B0%E5%A4%A7%E6%8E%92%E5%88%97&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false diff --git a/docs/chapter_backtracking/backtracking_algorithm.md b/docs/chapter_backtracking/backtracking_algorithm.md index ca4b19885..aac9e8cc9 100644 --- a/docs/chapter_backtracking/backtracking_algorithm.md +++ b/docs/chapter_backtracking/backtracking_algorithm.md @@ -403,6 +403,12 @@ } ``` +=== "Ruby" + + ```ruby title="" + + ``` + === "Zig" ```zig title="" diff --git a/docs/chapter_computational_complexity/space_complexity.md b/docs/chapter_computational_complexity/space_complexity.md index dffa376bb..fb57deddb 100755 --- a/docs/chapter_computational_complexity/space_complexity.md +++ b/docs/chapter_computational_complexity/space_complexity.md @@ -332,6 +332,12 @@ } ``` +=== "Ruby" + + ```ruby title="" + + ``` + === "Zig" ```zig title="" @@ -490,6 +496,12 @@ } ``` +=== "Ruby" + + ```ruby title="" + + ``` + === "Zig" ```zig title="" @@ -748,6 +760,12 @@ } ``` +=== "Ruby" + + ```ruby title="" + + ``` + === "Zig" ```zig title="" diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index f3291cbfe..9589795ce 100755 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -186,6 +186,12 @@ } ``` +=== "Ruby" + + ```ruby title="" + + ``` + === "Zig" ```zig title="" @@ -465,6 +471,12 @@ $$ } ``` +=== "Ruby" + + ```ruby title="" + + ``` + === "Zig" ```zig title="" @@ -673,6 +685,12 @@ $$ } ``` +=== "Ruby" + + ```ruby title="" + + ``` + === "Zig" ```zig title="" @@ -949,6 +967,12 @@ $T(n)$ 是一次函数,说明其运行时间的增长趋势是线性的,因 } ``` +=== "Ruby" + + ```ruby title="" + + ``` + === "Zig" ```zig title="" diff --git a/docs/chapter_data_structure/basic_data_types.md b/docs/chapter_data_structure/basic_data_types.md index c0ea6aec3..dd59b9c6d 100644 --- a/docs/chapter_data_structure/basic_data_types.md +++ b/docs/chapter_data_structure/basic_data_types.md @@ -163,6 +163,12 @@ val bools = BooleanArray(5) ``` +=== "Ruby" + + ```ruby title="" + + ``` + === "Zig" ```zig title="" diff --git a/docs/chapter_hashing/hash_algorithm.md b/docs/chapter_hashing/hash_algorithm.md index 0ffc96d0d..11d36033e 100644 --- a/docs/chapter_hashing/hash_algorithm.md +++ b/docs/chapter_hashing/hash_algorithm.md @@ -371,6 +371,12 @@ $$ // 节点对象 utils.ListNode@1d81eb93 的哈希值为 495053715 ``` +=== "Ruby" + + ```ruby title="built_in_hash.rb" + + ``` + === "Zig" ```zig title="built_in_hash.zig" diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index 66c5565b8..213fc31de 100755 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -290,6 +290,12 @@ map.remove(10583) ``` +=== "Ruby" + + ```ruby title="hash_map.rb" + + ``` + === "Zig" ```zig title="hash_map.zig" @@ -503,6 +509,12 @@ } ``` +=== "Ruby" + + ```ruby title="hash_map.rb" + + ``` + === "Zig" ```zig title="hash_map.zig" diff --git a/docs/chapter_heap/heap.md b/docs/chapter_heap/heap.md index 453ebe784..841dcbf98 100644 --- a/docs/chapter_heap/heap.md +++ b/docs/chapter_heap/heap.md @@ -402,6 +402,12 @@ minHeap = PriorityQueue(mutableListOf(1, 3, 2, 5, 4)) ``` +=== "Ruby" + + ```ruby title="heap.rb" + + ``` + === "Zig" ```zig title="heap.zig" diff --git a/docs/chapter_preface/suggestions.md b/docs/chapter_preface/suggestions.md index 272094bc3..4cf0cefb9 100644 --- a/docs/chapter_preface/suggestions.md +++ b/docs/chapter_preface/suggestions.md @@ -169,6 +169,12 @@ */ ``` +=== "Ruby" + + ```ruby title="" + + ``` + === "Zig" ```zig title="" diff --git a/docs/chapter_stack_and_queue/deque.md b/docs/chapter_stack_and_queue/deque.md index d65c51b4b..84aaf80e8 100644 --- a/docs/chapter_stack_and_queue/deque.md +++ b/docs/chapter_stack_and_queue/deque.md @@ -352,6 +352,12 @@ val isEmpty = deque.isEmpty() ``` +=== "Ruby" + + ```ruby title="deque.rb" + + ``` + === "Zig" ```zig title="deque.zig" diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index cf4aa5c01..1b78cd878 100755 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -328,6 +328,12 @@ val isEmpty = queue.isEmpty() ``` +=== "Ruby" + + ```ruby title="queue.rb" + + ``` + === "Zig" ```zig title="queue.zig" diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index 739158ceb..23d8d205b 100755 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -322,6 +322,12 @@ val isEmpty = stack.isEmpty() ``` +=== "Ruby" + + ```ruby title="stack.rb" + + ``` + === "Zig" ```zig title="stack.zig" diff --git a/docs/chapter_tree/array_representation_of_tree.md b/docs/chapter_tree/array_representation_of_tree.md index 6da64cb40..ad7619345 100644 --- a/docs/chapter_tree/array_representation_of_tree.md +++ b/docs/chapter_tree/array_representation_of_tree.md @@ -120,6 +120,12 @@ val tree = mutableListOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 ) ``` +=== "Ruby" + + ```ruby title="" + + ``` + === "Zig" ```zig title="" diff --git a/docs/chapter_tree/avl_tree.md b/docs/chapter_tree/avl_tree.md index 6c2ada4f7..ab41ab8e7 100644 --- a/docs/chapter_tree/avl_tree.md +++ b/docs/chapter_tree/avl_tree.md @@ -211,6 +211,12 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二 } ``` +=== "Ruby" + + ```ruby title="" + + ``` + === "Zig" ```zig title="" diff --git a/docs/chapter_tree/binary_tree.md b/docs/chapter_tree/binary_tree.md index 40a3b3eec..f8c67fe14 100644 --- a/docs/chapter_tree/binary_tree.md +++ b/docs/chapter_tree/binary_tree.md @@ -186,6 +186,12 @@ } ``` +=== "Ruby" + + ```ruby title="" + + ``` + === "Zig" ```zig title="" @@ -423,6 +429,12 @@ n2.right = n5 ``` +=== "Ruby" + + ```ruby title="binary_tree.rb" + + ``` + === "Zig" ```zig title="binary_tree.zig" @@ -579,6 +591,12 @@ n1.left = n2 ``` +=== "Ruby" + + ```ruby title="binary_tree.rb" + + ``` + === "Zig" ```zig title="binary_tree.zig" diff --git a/en/docs/chapter_array_and_linkedlist/array.md b/en/docs/chapter_array_and_linkedlist/array.md index 93dcbc60f..7a1a655a8 100755 --- a/en/docs/chapter_array_and_linkedlist/array.md +++ b/en/docs/chapter_array_and_linkedlist/array.md @@ -119,14 +119,6 @@ Arrays can be initialized in two ways depending on the needs: either without ini var nums = [_]i32{ 1, 3, 2, 5, 4 }; ``` -=== "Ruby" - - ```ruby title="array.rb" - # Initialize array - arr = Array.new(5, 0) # [ 0, 0, 0, 0, 0 ] - nums = [1, 3, 2, 5, 4] - ``` - ### Accessing Elements Elements in an array are stored in contiguous memory spaces, making it simpler to compute each element's memory address. The formula shown in the Figure below aids in determining an element's memory address, utilizing the array's memory address (specifically, the first element's address) and the element's index. This computation streamlines direct access to the desired element. diff --git a/en/docs/chapter_array_and_linkedlist/linked_list.md b/en/docs/chapter_array_and_linkedlist/linked_list.md index 9a61033e0..93e7c591d 100755 --- a/en/docs/chapter_array_and_linkedlist/linked_list.md +++ b/en/docs/chapter_array_and_linkedlist/linked_list.md @@ -185,22 +185,6 @@ As the code below illustrates, a `ListNode` in a linked list, besides holding a } ``` -=== "Ruby" - - ```ruby title="" - # Linked List Node Class - class ListNode - attr_accessor :val # Node value - attr_accessor :next # Reference to the next node - - def initialize(val=nil, next_node=nil) - @val = val || 0 - @next = next_node - end - end - ``` - - ## Common Operations on Linked Lists ### Initializing a Linked List @@ -418,23 +402,6 @@ Constructing a linked list is a two-step process: first, initializing each node n3.next = &n4; ``` -=== "Ruby" - - ```ruby title=linked_list.rb - # Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 - # Initialize each node - n0 = ListNode.new 1 - n1 = ListNode.new 3 - n2 = ListNode.new 2 - n3 = ListNode.new 5 - n4 = ListNode.new 4 - # Build references between nodes - n0.next = n1 - n1.next = n2 - n2.next = n3 - n3.next = n4 - ``` - The array as a whole is a variable, for instance, the array `nums` includes elements like `nums[0]`, `nums[1]`, and so on, whereas a linked list is made up of several distinct node objects. **We typically refer to a linked list by its head node**, for example, the linked list in the previous code snippet is referred to as `n0`. ### Inserting a Node @@ -697,23 +664,6 @@ As shown in the figure, there are three common types of linked lists. } ``` -=== "Ruby" - - ```ruby title="" - # Bidirectional linked list node class - class ListNode - attr_accessor :val # Node value - attr_accessor :next # Reference to the successor node - attr_accessor :prev # Reference to the predecessor node - - def initialize(val=nil, next_node=nil, prev_node=nil) - @val = val || 0 - @next = next_node - @prev = prev_node - end - end - ``` - ![Common Types of Linked Lists](linked_list.assets/linkedlist_common_types.png) ## Typical Applications of Linked Lists diff --git a/en/docs/chapter_array_and_linkedlist/list.md b/en/docs/chapter_array_and_linkedlist/list.md index 5520cc932..028d9265e 100755 --- a/en/docs/chapter_array_and_linkedlist/list.md +++ b/en/docs/chapter_array_and_linkedlist/list.md @@ -141,16 +141,6 @@ We typically use two initialization methods: "without initial values" and "with try nums.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 }); ``` -=== "Ruby" - - ```ruby title="list.rb" - # Initialize list - # Without initial values - nums1 = [] - # With initial values - nums = [1, 3, 2, 5, 4] - ``` - ### Accessing Elements Lists are essentially arrays, thus they can access and update elements in $O(1)$ time, which is very efficient. @@ -276,15 +266,6 @@ Lists are essentially arrays, thus they can access and update elements in $O(1)$ nums.items[1] = 0; // Update the element at index 1 to 0 ``` -=== "Ruby" - - ```ruby title="list.rb" - # Access elements - num = nums[1] - # Update elements - nums[1] = 0 - ``` - ### Inserting and Removing Elements Compared to arrays, lists offer more flexibility in adding and removing elements. While adding elements to the end of a list is an $O(1)$ operation, the efficiency of inserting and removing elements elsewhere in the list remains the same as in arrays, with a time complexity of $O(n)$. @@ -521,26 +502,6 @@ Compared to arrays, lists offer more flexibility in adding and removing elements _ = nums.orderedRemove(3); // Remove the element at index 3 ``` -=== "Ruby" - - ```ruby title="list.rb" - # Clear list - nums.clear - - # Append elements at the end - nums << 1 - nums << 3 - nums << 2 - nums << 5 - nums << 4 - - # Insert element in the middle - nums.insert 3, 6 # Insert number 6 at index 3 - - # Remove elements - nums.delete_at 3 # Remove the element at index 3 - ``` - ### Iterating the List Similar to arrays, lists can be iterated either by using indices or by directly iterating through each element. @@ -730,22 +691,6 @@ Similar to arrays, lists can be iterated either by using indices or by directly } ``` -=== "Ruby" - - ```ruby title="list.rb" - # Iterate through the list by index - count = 0 - for i in 0...nums.length - count += nums[i] - end - - # Iterate directly though list elements - count = 0 - for num in nums - count += num - end - ``` - ### Concatenating Lists Given a new list `nums1`, we can append it to the end of the original list. @@ -853,14 +798,6 @@ Given a new list `nums1`, we can append it to the end of the original list. try nums.insertSlice(nums.items.len, nums1.items); // Concatenate nums1 to the end of nums ``` -=== "Ruby" - - ```ruby title="list.rb" - # Concatenate two lists - nums1 = [6, 8, 7, 10, 9] - nums += nums1 - ``` - ### Sorting the List Once the list is sorted, we can employ algorithms commonly used in array-related algorithm problems, such as "binary search" and "two-pointer" algorithms. @@ -954,13 +891,6 @@ Once the list is sorted, we can employ algorithms commonly used in array-related std.sort.sort(i32, nums.items, {}, comptime std.sort.asc(i32)); ``` -=== "Ruby" - - ```ruby title="list.rb" - # Sort the list - nums = nums.sort { |a, b| a <=> b } - ``` - ## List Implementation Many programming languages come with built-in lists, including Java, C++, Python, etc. Their implementations tend to be intricate, featuring carefully considered settings for various parameters, like initial capacity and expansion factors. Readers who are curious can delve into the source code for further learning. diff --git a/overrides/stylesheets/extra.css b/overrides/stylesheets/extra.css index 67f0d38d2..84da9d4e2 100644 --- a/overrides/stylesheets/extra.css +++ b/overrides/stylesheets/extra.css @@ -209,7 +209,7 @@ body { /* code block tabs */ .md-typeset .tabbed-labels>label { - font-size: 0.59rem; + font-size: 0.545rem; } /* header banner */