mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-28 16:44:22 +00:00
code: update zig 0.14.1 for the chapter of array_and_linkedlist and computational_complexity (#1787)
* update zig array list chapter * update not need change codes. * fix some pr issues and update time space chapter
This commit is contained in:
@@ -1,26 +1,28 @@
|
||||
// File: array.zig
|
||||
// Created Time: 2023-01-07
|
||||
// Author: codingonion (coderonion@gmail.com)
|
||||
// Author: codingonion (coderonion@gmail.com), CreatorMetaSky (creator_meta_sky@163.com)
|
||||
|
||||
const std = @import("std");
|
||||
const inc = @import("include");
|
||||
const utils = @import("utils");
|
||||
|
||||
// 随机访问元素
|
||||
pub fn randomAccess(nums: []i32) i32 {
|
||||
pub fn randomAccess(nums: []const i32) i32 {
|
||||
// 在区间 [0, nums.len) 中随机抽取一个整数
|
||||
var randomIndex = std.crypto.random.intRangeLessThan(usize, 0, nums.len);
|
||||
const random_index = std.crypto.random.intRangeLessThan(usize, 0, nums.len);
|
||||
// 获取并返回随机元素
|
||||
var randomNum = nums[randomIndex];
|
||||
const randomNum = nums[random_index];
|
||||
return randomNum;
|
||||
}
|
||||
|
||||
// 扩展数组长度
|
||||
pub fn extend(mem_allocator: std.mem.Allocator, nums: []i32, enlarge: usize) ![]i32 {
|
||||
pub fn extend(allocator: std.mem.Allocator, nums: []const i32, enlarge: usize) ![]i32 {
|
||||
// 初始化一个扩展长度后的数组
|
||||
var res = try mem_allocator.alloc(i32, nums.len + enlarge);
|
||||
const res = try allocator.alloc(i32, nums.len + enlarge);
|
||||
@memset(res, 0);
|
||||
|
||||
// 将原数组中的所有元素复制到新数组
|
||||
std.mem.copy(i32, res, nums);
|
||||
std.mem.copyForwards(i32, res, nums);
|
||||
|
||||
// 返回扩展后的新数组
|
||||
return res;
|
||||
}
|
||||
@@ -46,18 +48,26 @@ pub fn remove(nums: []i32, index: usize) void {
|
||||
}
|
||||
|
||||
// 遍历数组
|
||||
pub fn traverse(nums: []i32) void {
|
||||
pub fn traverse(nums: []const i32) void {
|
||||
var count: i32 = 0;
|
||||
|
||||
// 通过索引遍历数组
|
||||
var i: i32 = 0;
|
||||
var i: usize = 0;
|
||||
while (i < nums.len) : (i += 1) {
|
||||
count += nums[i];
|
||||
}
|
||||
count = 0;
|
||||
|
||||
// 直接遍历数组元素
|
||||
count = 0;
|
||||
for (nums) |num| {
|
||||
count += num;
|
||||
}
|
||||
|
||||
// 同时遍历数据索引和元素
|
||||
for (nums, 0..) |num, index| {
|
||||
count += nums[index];
|
||||
count += num;
|
||||
}
|
||||
}
|
||||
|
||||
// 在数组中查找指定元素
|
||||
@@ -69,49 +79,53 @@ pub fn find(nums: []i32, target: i32) i32 {
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
pub fn main() !void {
|
||||
// 初始化内存分配器
|
||||
var mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
defer mem_arena.deinit();
|
||||
const mem_allocator = mem_arena.allocator();
|
||||
|
||||
pub fn run() !void {
|
||||
// 初始化数组
|
||||
var arr = [_]i32{0} ** 5;
|
||||
std.debug.print("数组 arr = ", .{});
|
||||
inc.PrintUtil.printArray(i32, &arr);
|
||||
const arr = [_]i32{0} ** 5;
|
||||
std.debug.print("数组 arr = {}\n", .{utils.fmt.slice(&arr)});
|
||||
|
||||
// 数组切片
|
||||
var array = [_]i32{ 1, 3, 2, 5, 4 };
|
||||
var known_at_runtime_zero: usize = 0;
|
||||
var nums = array[known_at_runtime_zero..];
|
||||
std.debug.print("\n数组 nums = ", .{});
|
||||
inc.PrintUtil.printArray(i32, nums);
|
||||
_ = &known_at_runtime_zero;
|
||||
var nums = array[known_at_runtime_zero..array.len]; // 通过 known_at_runtime_zero 运行时变量将指针变切片
|
||||
std.debug.print("数组 nums = {}\n", .{utils.fmt.slice(nums)});
|
||||
|
||||
// 随机访问
|
||||
var randomNum = randomAccess(nums);
|
||||
std.debug.print("\n在 nums 中获取随机元素 {}", .{randomNum});
|
||||
const randomNum = randomAccess(nums);
|
||||
std.debug.print("在 nums 中获取随机元素 {}\n", .{randomNum});
|
||||
|
||||
// 初始化内存分配器
|
||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
defer arena.deinit();
|
||||
const allocator = arena.allocator();
|
||||
|
||||
// 长度扩展
|
||||
nums = try extend(mem_allocator, nums, 3);
|
||||
std.debug.print("\n将数组长度扩展至 8 ,得到 nums = ", .{});
|
||||
inc.PrintUtil.printArray(i32, nums);
|
||||
nums = try extend(allocator, nums, 3);
|
||||
std.debug.print("将数组长度扩展至 8 ,得到 nums = {}\n", .{utils.fmt.slice(nums)});
|
||||
|
||||
// 插入元素
|
||||
insert(nums, 6, 3);
|
||||
std.debug.print("\n在索引 3 处插入数字 6 ,得到 nums = ", .{});
|
||||
inc.PrintUtil.printArray(i32, nums);
|
||||
std.debug.print("在索引 3 处插入数字 6 ,得到 nums = {}\n", .{utils.fmt.slice(nums)});
|
||||
|
||||
// 删除元素
|
||||
remove(nums, 2);
|
||||
std.debug.print("\n删除索引 2 处的元素,得到 nums = ", .{});
|
||||
inc.PrintUtil.printArray(i32, nums);
|
||||
std.debug.print("删除索引 2 处的元素,得到 nums = {}\n", .{utils.fmt.slice(nums)});
|
||||
|
||||
// 遍历数组
|
||||
traverse(nums);
|
||||
|
||||
// 查找元素
|
||||
var index = find(nums, 3);
|
||||
std.debug.print("\n在 nums 中查找元素 3 ,得到索引 = {}\n", .{index});
|
||||
const index = find(nums, 3);
|
||||
std.debug.print("在 nums 中查找元素 3 ,得到索引 = {}\n", .{index});
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
try run();
|
||||
}
|
||||
|
||||
test "array" {
|
||||
try run();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user