mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	update zig codes for Section 'Space Complexity' and 'Space Time Tradeoff'
This commit is contained in:
		@ -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");
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
 | 
			
		||||
@ -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});
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
@ -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();
 | 
			
		||||
 | 
			
		||||
@ -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) {
 | 
			
		||||
 | 
			
		||||
@ -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();
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user