From 2572b83540edc08e2e64d9e0d2aed2489f371e7e Mon Sep 17 00:00:00 2001 From: sjinzh <99076655+sjinzh@users.noreply.github.com> Date: Tue, 10 Jan 2023 17:19:21 +0800 Subject: [PATCH] update zig codes for Section 'Space Complexity' and 'Space Time Tradeoff' --- codes/zig/build.zig | 8 ++++---- .../chapter_computational_complexity/leetcode_two_sum.zig | 4 ++++ .../chapter_computational_complexity/space_complexity.zig | 4 ++++ .../chapter_computational_complexity/time_complexity.zig | 4 ++++ .../worst_best_time_complexity.zig | 4 ++++ codes/zig/include/ListNode.zig | 1 + codes/zig/include/PrintUtil.zig | 1 + codes/zig/include/TreeNode.zig | 1 + 8 files changed, 23 insertions(+), 4 deletions(-) diff --git a/codes/zig/build.zig b/codes/zig/build.zig index e72ca4a5..9584ba4b 100644 --- a/codes/zig/build.zig +++ b/codes/zig/build.zig @@ -11,7 +11,7 @@ pub fn build(b: *std.build.Builder) void { const mode = b.standardReleaseOptions(); // Section: "Time Complexity" - // File: "chapter_computational_complexity/time_complexity.zig" + // Source File: "chapter_computational_complexity/time_complexity.zig" // Run Command: zig build run_time_complexity const exe_time_complexity = b.addExecutable("time_complexity", "chapter_computational_complexity/time_complexity.zig"); exe_time_complexity.addPackagePath("include", "include/include.zig"); @@ -24,7 +24,7 @@ pub fn build(b: *std.build.Builder) void { const run_step_time_complexity = b.step("run_time_complexity", "Run time_complexity"); run_step_time_complexity.dependOn(&run_cmd_time_complexity.step); - // File: "chapter_computational_complexity/worst_best_time_complexity.zig" + // Source File: "chapter_computational_complexity/worst_best_time_complexity.zig" // Run Command: zig build run_worst_best_time_complexity const exe_worst_best_time_complexity = b.addExecutable("worst_best_time_complexity", "chapter_computational_complexity/worst_best_time_complexity.zig"); exe_worst_best_time_complexity.addPackagePath("include", "include/include.zig"); @@ -38,7 +38,7 @@ pub fn build(b: *std.build.Builder) void { run_step_worst_best_time_complexity.dependOn(&run_cmd_worst_best_time_complexity.step); // Section: "Space Complexity" - // File: "chapter_computational_complexity/space_complexity.zig" + // Source File: "chapter_computational_complexity/space_complexity.zig" // Run Command: zig build run_space_complexity const exe_space_complexity = b.addExecutable("space_complexity", "chapter_computational_complexity/space_complexity.zig"); exe_space_complexity.addPackagePath("include", "include/include.zig"); @@ -52,7 +52,7 @@ pub fn build(b: *std.build.Builder) void { run_step_space_complexity.dependOn(&run_cmd_space_complexity.step); // Section: "Space Time Tradeoff" - // File: "chapter_computational_complexity/leetcode_two_sum.zig" + // Source File: "chapter_computational_complexity/leetcode_two_sum.zig" // Run Command: zig build run_leetcode_two_sum const exe_leetcode_two_sum = b.addExecutable("leetcode_two_sum", "chapter_computational_complexity/leetcode_two_sum.zig"); exe_leetcode_two_sum.addPackagePath("include", "include/include.zig"); diff --git a/codes/zig/chapter_computational_complexity/leetcode_two_sum.zig b/codes/zig/chapter_computational_complexity/leetcode_two_sum.zig index 66b96f95..dc963b59 100644 --- a/codes/zig/chapter_computational_complexity/leetcode_two_sum.zig +++ b/codes/zig/chapter_computational_complexity/leetcode_two_sum.zig @@ -44,6 +44,10 @@ const SolutionHashMap = struct { // Driver Code pub fn main() !void { + // 查看本地CPU架构和操作系统信息 + var native_target_info = try std.zig.system.NativeTargetInfo.detect(std.zig.CrossTarget{}); + std.debug.print("Native Info: CPU Arch = {}, OS = {}\n", .{native_target_info.target.cpu.arch, native_target_info.target.os.tag}); + // ======= Test Case ======= var nums = [_]i32{ 2, 7, 11, 15 }; var target: i32 = 9; diff --git a/codes/zig/chapter_computational_complexity/space_complexity.zig b/codes/zig/chapter_computational_complexity/space_complexity.zig index 9798821a..3fe81a70 100644 --- a/codes/zig/chapter_computational_complexity/space_complexity.zig +++ b/codes/zig/chapter_computational_complexity/space_complexity.zig @@ -102,6 +102,10 @@ fn buildTree(mem_allocator: std.mem.Allocator, n: i32) !?*inc.TreeNode(i32) { // Driver Code pub fn main() !void { + // 查看本地CPU架构和操作系统信息 + var native_target_info = try std.zig.system.NativeTargetInfo.detect(std.zig.CrossTarget{}); + std.debug.print("Native Info: CPU Arch = {}, OS = {}\n", .{native_target_info.target.cpu.arch, native_target_info.target.os.tag}); + const n: i32 = 5; // 常数阶 constant(n); diff --git a/codes/zig/chapter_computational_complexity/time_complexity.zig b/codes/zig/chapter_computational_complexity/time_complexity.zig index dab17034..ef8d7fe6 100644 --- a/codes/zig/chapter_computational_complexity/time_complexity.zig +++ b/codes/zig/chapter_computational_complexity/time_complexity.zig @@ -139,6 +139,10 @@ fn factorialRecur(n: i32) i32 { // Driver Code pub fn main() void { + // 查看本地CPU架构和操作系统信息 + var native_target_info = try std.zig.system.NativeTargetInfo.detect(std.zig.CrossTarget{}); + std.debug.print("Native Info: CPU Arch = {}, OS = {}\n", .{native_target_info.target.cpu.arch, native_target_info.target.os.tag}); + // 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势 const n: i32 = 8; std.debug.print("输入数据大小 n = {}\n", .{n}); diff --git a/codes/zig/chapter_computational_complexity/worst_best_time_complexity.zig b/codes/zig/chapter_computational_complexity/worst_best_time_complexity.zig index bd2a1464..42961aaf 100644 --- a/codes/zig/chapter_computational_complexity/worst_best_time_complexity.zig +++ b/codes/zig/chapter_computational_complexity/worst_best_time_complexity.zig @@ -28,6 +28,10 @@ pub fn findOne(nums: []i32) i32 { // Driver Code pub fn main() void { + // 查看本地CPU架构和操作系统信息 + var native_target_info = try std.zig.system.NativeTargetInfo.detect(std.zig.CrossTarget{}); + std.debug.print("Native Info: CPU Arch = {}, OS = {}\n", .{native_target_info.target.cpu.arch, native_target_info.target.os.tag}); + var i: i32 = 0; while (i < 10) : (i += 1) { const n: usize = 100; diff --git a/codes/zig/include/ListNode.zig b/codes/zig/include/ListNode.zig index 8ce01d3e..ae0246c0 100644 --- a/codes/zig/include/ListNode.zig +++ b/codes/zig/include/ListNode.zig @@ -5,6 +5,7 @@ const std = @import("std"); // Definition for a singly-linked list node +// 编译期泛型 pub fn ListNode(comptime T: type) type { return struct { const Self = @This(); diff --git a/codes/zig/include/PrintUtil.zig b/codes/zig/include/PrintUtil.zig index 8a7dca7a..9e6b18b0 100644 --- a/codes/zig/include/PrintUtil.zig +++ b/codes/zig/include/PrintUtil.zig @@ -7,6 +7,7 @@ const ListNode = @import("ListNode.zig").ListNode; const TreeNode = @import("TreeNode.zig").TreeNode; // Print an array +// 编译期泛型 pub fn printArray(comptime T: type, nums: []T) void { std.debug.print("[", .{}); if (nums.len > 0) { diff --git a/codes/zig/include/TreeNode.zig b/codes/zig/include/TreeNode.zig index 9bd51ba3..12af9e8f 100644 --- a/codes/zig/include/TreeNode.zig +++ b/codes/zig/include/TreeNode.zig @@ -5,6 +5,7 @@ const std = @import("std"); // Definition for a binary tree node +// 编译期泛型 pub fn TreeNode(comptime T: type) type { return struct { const Self = @This();