Merge pull request #228 from sjinzh/master
add zig codes for Section 'Space Complexity' and 'Space Time Tradeoff'pull/236/head
commit
6a46e38e1b
@ -1,46 +1,67 @@
|
||||
// File: build.zig
|
||||
// Created Time: 2023-01-07
|
||||
// Author: sjinzh (sjinzh@gmail.com)
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
// zig version 0.10.0
|
||||
// Zig Version: 0.10.0
|
||||
// Build Command: zig build
|
||||
pub fn build(b: *std.build.Builder) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const mode = b.standardReleaseOptions();
|
||||
|
||||
// "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");
|
||||
exe_time_complexity.setTarget(target);
|
||||
exe_time_complexity.setBuildMode(mode);
|
||||
exe_time_complexity.install();
|
||||
const run_cmd_time_complexity = exe_time_complexity.run();
|
||||
run_cmd_time_complexity.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_time_complexity.addArgs(args);
|
||||
const run_step_time_complexity = b.step("run_time_complexity", "Run time_complexity");
|
||||
run_step_time_complexity.dependOn(&run_cmd_time_complexity.step);
|
||||
// Section: "Time Complexity"
|
||||
// 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");
|
||||
exe_time_complexity.setTarget(target);
|
||||
exe_time_complexity.setBuildMode(mode);
|
||||
exe_time_complexity.install();
|
||||
const run_cmd_time_complexity = exe_time_complexity.run();
|
||||
run_cmd_time_complexity.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_time_complexity.addArgs(args);
|
||||
const run_step_time_complexity = b.step("run_time_complexity", "Run time_complexity");
|
||||
run_step_time_complexity.dependOn(&run_cmd_time_complexity.step);
|
||||
|
||||
// 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");
|
||||
exe_worst_best_time_complexity.setTarget(target);
|
||||
exe_worst_best_time_complexity.setBuildMode(mode);
|
||||
exe_worst_best_time_complexity.install();
|
||||
const run_cmd_worst_best_time_complexity = exe_worst_best_time_complexity.run();
|
||||
run_cmd_worst_best_time_complexity.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_worst_best_time_complexity.addArgs(args);
|
||||
const run_step_worst_best_time_complexity = b.step("run_worst_best_time_complexity", "Run worst_best_time_complexity");
|
||||
run_step_worst_best_time_complexity.dependOn(&run_cmd_worst_best_time_complexity.step);
|
||||
|
||||
// "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");
|
||||
exe_worst_best_time_complexity.setTarget(target);
|
||||
exe_worst_best_time_complexity.setBuildMode(mode);
|
||||
exe_worst_best_time_complexity.install();
|
||||
const run_cmd_worst_best_time_complexity = exe_worst_best_time_complexity.run();
|
||||
run_cmd_worst_best_time_complexity.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_worst_best_time_complexity.addArgs(args);
|
||||
const run_step_worst_best_time_complexity = b.step("run_worst_best_time_complexity", "Run worst_best_time_complexity");
|
||||
run_step_worst_best_time_complexity.dependOn(&run_cmd_worst_best_time_complexity.step);
|
||||
// Section: "Space Complexity"
|
||||
// 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");
|
||||
exe_space_complexity.setTarget(target);
|
||||
exe_space_complexity.setBuildMode(mode);
|
||||
exe_space_complexity.install();
|
||||
const run_cmd_space_complexity = exe_space_complexity.run();
|
||||
run_cmd_space_complexity.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_space_complexity.addArgs(args);
|
||||
const run_step_space_complexity = b.step("run_space_complexity", "Run space_complexity");
|
||||
run_step_space_complexity.dependOn(&run_cmd_space_complexity.step);
|
||||
|
||||
// "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");
|
||||
exe_leetcode_two_sum.setTarget(target);
|
||||
exe_leetcode_two_sum.setBuildMode(mode);
|
||||
exe_leetcode_two_sum.install();
|
||||
const run_cmd_leetcode_two_sum = exe_leetcode_two_sum.run();
|
||||
run_cmd_leetcode_two_sum.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_leetcode_two_sum.addArgs(args);
|
||||
const run_step_leetcode_two_sum = b.step("run_leetcode_two_sum", "Run leetcode_two_sum");
|
||||
run_step_leetcode_two_sum.dependOn(&run_cmd_leetcode_two_sum.step);
|
||||
// Section: "Space Time Tradeoff"
|
||||
// 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");
|
||||
exe_leetcode_two_sum.setTarget(target);
|
||||
exe_leetcode_two_sum.setBuildMode(mode);
|
||||
exe_leetcode_two_sum.install();
|
||||
const run_cmd_leetcode_two_sum = exe_leetcode_two_sum.run();
|
||||
run_cmd_leetcode_two_sum.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_leetcode_two_sum.addArgs(args);
|
||||
const run_step_leetcode_two_sum = b.step("run_leetcode_two_sum", "Run leetcode_two_sum");
|
||||
run_step_leetcode_two_sum.dependOn(&run_cmd_leetcode_two_sum.step);
|
||||
}
|
||||
|
@ -0,0 +1,21 @@
|
||||
// File: ListNode.zig
|
||||
// Created Time: 2023-01-07
|
||||
// Author: sjinzh (sjinzh@gmail.com)
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
// Definition for a singly-linked list node
|
||||
// 编译期泛型
|
||||
pub fn ListNode(comptime T: type) type {
|
||||
return struct {
|
||||
const Self = @This();
|
||||
|
||||
val: T = 0,
|
||||
next: ?*Self = null,
|
||||
|
||||
// Initialize a list node with specific value
|
||||
pub fn init(self: *Self, x: i32) void {
|
||||
self.val = x;
|
||||
}
|
||||
};
|
||||
}
|
@ -1,13 +1,73 @@
|
||||
// File: TreeNode.zig
|
||||
// File: PrintUtil.zig
|
||||
// Created Time: 2023-01-07
|
||||
// Author: sjinzh (sjinzh@gmail.com)
|
||||
|
||||
const std = @import("std");
|
||||
const ListNode = @import("ListNode.zig").ListNode;
|
||||
const TreeNode = @import("TreeNode.zig").TreeNode;
|
||||
|
||||
// Print an Array
|
||||
// Print an array
|
||||
// 编译期泛型
|
||||
pub fn printArray(comptime T: type, nums: []T) void {
|
||||
std.debug.print("[", .{});
|
||||
for (nums) |num, j| {
|
||||
std.debug.print("{}{s}", .{num, if (j == nums.len-1) "]\n" else ", " });
|
||||
}
|
||||
if (nums.len > 0) {
|
||||
for (nums) |num, j| {
|
||||
std.debug.print("{}{s}", .{num, if (j == nums.len-1) "]\n" else ", " });
|
||||
}
|
||||
} else {
|
||||
std.debug.print("]", .{});
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
}
|
||||
|
||||
// This tree printer is borrowed from TECHIE DELIGHT
|
||||
// https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
const Trunk = struct {
|
||||
prev: ?*Trunk = null,
|
||||
str: []const u8 = undefined,
|
||||
|
||||
pub fn init(self: *Trunk, prev: ?*Trunk, str: []const u8) void {
|
||||
self.prev = prev;
|
||||
self.str = str;
|
||||
}
|
||||
};
|
||||
|
||||
// Helper function to print branches of the binary tree
|
||||
pub fn showTrunks(p: ?*Trunk) void {
|
||||
if (p == null) return;
|
||||
showTrunks(p.?.prev);
|
||||
std.debug.print("{s}", .{p.?.str});
|
||||
}
|
||||
|
||||
// The interface of the tree printer
|
||||
// Print a binary tree
|
||||
pub fn printTree(root: ?*TreeNode(i32), prev: ?*Trunk, isLeft: bool) !void {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
var prev_str = " ";
|
||||
var trunk = Trunk{.prev = prev, .str = prev_str};
|
||||
|
||||
try printTree(root.?.right, &trunk, true);
|
||||
|
||||
if (prev == null) {
|
||||
trunk.str = "———";
|
||||
} else if (isLeft) {
|
||||
trunk.str = "/———";
|
||||
prev_str = " |";
|
||||
} else {
|
||||
trunk.str = "\\———";
|
||||
prev.?.str = prev_str;
|
||||
}
|
||||
|
||||
showTrunks(&trunk);
|
||||
std.debug.print(" {}\n", .{root.?.val});
|
||||
|
||||
if (prev) |_| {
|
||||
prev.?.str = prev_str;
|
||||
}
|
||||
trunk.str = " |";
|
||||
|
||||
try printTree(root.?.left, &trunk, false);
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
// File: TreeNode.zig
|
||||
// Created Time: 2023-01-07
|
||||
// Author: sjinzh (sjinzh@gmail.com)
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
// Definition for a binary tree node
|
||||
// 编译期泛型
|
||||
pub fn TreeNode(comptime T: type) type {
|
||||
return struct {
|
||||
const Self = @This();
|
||||
|
||||
val: T = undefined,
|
||||
left: ?*Self = null,
|
||||
right: ?*Self = null,
|
||||
|
||||
// Initialize a tree node with specific value
|
||||
pub fn init(self: *Self, x: i32) void {
|
||||
self.val = x;
|
||||
}
|
||||
};
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
// File: include.zig
|
||||
// Created Time: 2023-01-04
|
||||
// Created Time: 2023-01-07
|
||||
// Author: sjinzh (sjinzh@gmail.com)
|
||||
|
||||
pub const PrintUtil = @import("PrintUtil.zig");
|
||||
pub const PrintUtil = @import("PrintUtil.zig");
|
||||
pub const ListNode = @import("ListNode.zig").ListNode;
|
||||
pub const TreeNode = @import("TreeNode.zig").TreeNode;
|
Loading…
Reference in new issue