mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-01 17:53:18 +08:00
feat: Revised the book (#978)
* Sync recent changes to the revised Word. * Revised the preface chapter * Revised the introduction chapter * Revised the computation complexity chapter * Revised the chapter data structure * Revised the chapter array and linked list * Revised the chapter stack and queue * Revised the chapter hashing * Revised the chapter tree * Revised the chapter heap * Revised the chapter graph * Revised the chapter searching * Reivised the sorting chapter * Revised the divide and conquer chapter * Revised the chapter backtacking * Revised the DP chapter * Revised the greedy chapter * Revised the appendix chapter * Revised the preface chapter doubly * Revised the figures
This commit is contained in:
@@ -32,11 +32,11 @@ pub fn insert(nums: []i32, num: i32, index: usize) void {
|
||||
while (i > index) : (i -= 1) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// 将 num 赋给 index 处元素
|
||||
// 将 num 赋给 index 处的元素
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
// 删除索引 index 处元素
|
||||
// 删除索引 index 处的元素
|
||||
pub fn remove(nums: []i32, index: usize) void {
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
var i = index;
|
||||
|
||||
@@ -53,7 +53,7 @@ pub fn main() !void {
|
||||
var n2 = inc.ListNode(i32){.val = 2};
|
||||
var n3 = inc.ListNode(i32){.val = 5};
|
||||
var n4 = inc.ListNode(i32){.val = 4};
|
||||
// 构建引用指向
|
||||
// 构建节点之间的引用
|
||||
n0.next = &n1;
|
||||
n1.next = &n2;
|
||||
n2.next = &n3;
|
||||
|
||||
@@ -29,7 +29,7 @@ pub fn main() !void {
|
||||
std.debug.print("\n清空列表后 nums = ", .{});
|
||||
inc.PrintUtil.printList(i32, nums);
|
||||
|
||||
// 尾部添加元素
|
||||
// 在尾部添加元素
|
||||
try nums.append(1);
|
||||
try nums.append(3);
|
||||
try nums.append(2);
|
||||
@@ -38,7 +38,7 @@ pub fn main() !void {
|
||||
std.debug.print("\n添加元素后 nums = ", .{});
|
||||
inc.PrintUtil.printList(i32, nums);
|
||||
|
||||
// 中间插入元素
|
||||
// 在中间插入元素
|
||||
try nums.insert(3, 6);
|
||||
std.debug.print("\n在索引 3 处插入数字 6 ,得到 nums = ", .{});
|
||||
inc.PrintUtil.printList(i32, nums);
|
||||
|
||||
@@ -5,14 +5,14 @@
|
||||
const std = @import("std");
|
||||
const inc = @import("include");
|
||||
|
||||
// 列表类简易实现
|
||||
// 列表类
|
||||
pub fn MyList(comptime T: type) type {
|
||||
return struct {
|
||||
const Self = @This();
|
||||
|
||||
arr: []T = undefined, // 数组(存储列表元素)
|
||||
arrCapacity: usize = 10, // 列表容量
|
||||
numSize: usize = 0, // 列表长度(即当前元素数量)
|
||||
numSize: usize = 0, // 列表长度(当前元素数量)
|
||||
extendRatio: usize = 2, // 每次列表扩容的倍数
|
||||
mem_arena: ?std.heap.ArenaAllocator = null,
|
||||
mem_allocator: std.mem.Allocator = undefined, // 内存分配器
|
||||
@@ -33,7 +33,7 @@ pub fn MyList(comptime T: type) type {
|
||||
self.mem_arena.?.deinit();
|
||||
}
|
||||
|
||||
// 获取列表长度(即当前元素数量)
|
||||
// 获取列表长度(当前元素数量)
|
||||
pub fn size(self: *Self) usize {
|
||||
return self.numSize;
|
||||
}
|
||||
@@ -57,7 +57,7 @@ pub fn MyList(comptime T: type) type {
|
||||
self.arr[index] = num;
|
||||
}
|
||||
|
||||
// 尾部添加元素
|
||||
// 在尾部添加元素
|
||||
pub fn add(self: *Self, num: T) !void {
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (self.size() == self.capacity()) try self.extendCapacity();
|
||||
@@ -66,7 +66,7 @@ pub fn MyList(comptime T: type) type {
|
||||
self.numSize += 1;
|
||||
}
|
||||
|
||||
// 中间插入元素
|
||||
// 在中间插入元素
|
||||
pub fn insert(self: *Self, index: usize, num: T) !void {
|
||||
if (index < 0 or index >= self.size()) @panic("索引越界");
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
@@ -130,7 +130,7 @@ pub fn main() !void {
|
||||
// 延迟释放内存
|
||||
defer nums.deinit();
|
||||
|
||||
// 尾部添加元素
|
||||
// 在尾部添加元素
|
||||
try nums.add(1);
|
||||
try nums.add(3);
|
||||
try nums.add(2);
|
||||
@@ -140,7 +140,7 @@ pub fn main() !void {
|
||||
inc.PrintUtil.printArray(i32, try nums.toArray());
|
||||
std.debug.print(" ,容量 = {} ,长度 = {}", .{nums.capacity(), nums.size()});
|
||||
|
||||
// 中间插入元素
|
||||
// 在中间插入元素
|
||||
try nums.insert(3, 6);
|
||||
std.debug.print("\n在索引 3 处插入数字 6 ,得到 nums = ", .{});
|
||||
inc.PrintUtil.printArray(i32, try nums.toArray());
|
||||
|
||||
Reference in New Issue
Block a user