diff --git a/codes/zig/.gitignore b/codes/zig/.gitignore new file mode 100644 index 00000000..4a0641ed --- /dev/null +++ b/codes/zig/.gitignore @@ -0,0 +1,2 @@ +zig-cache/ +zig-out/ \ No newline at end of file diff --git a/codes/zig/build.zig b/codes/zig/build.zig new file mode 100644 index 00000000..b04b3fd1 --- /dev/null +++ b/codes/zig/build.zig @@ -0,0 +1,46 @@ +const std = @import("std"); + +// zig version 0.10.0 +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); + + // "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/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); +} diff --git a/codes/zig/chapter_computational_complexity/leetcode_two_sum.zig b/codes/zig/chapter_computational_complexity/leetcode_two_sum.zig new file mode 100644 index 00000000..66b96f95 --- /dev/null +++ b/codes/zig/chapter_computational_complexity/leetcode_two_sum.zig @@ -0,0 +1,61 @@ +// File: leetcode_two_sum.zig +// Created Time: 2023-01-07 +// Author: sjinzh (sjinzh@gmail.com) + +const std = @import("std"); +const inc = @import("include"); + +const SolutionBruteForce = struct { + pub fn twoSum(self: *SolutionBruteForce, nums: []i32, target: i32) [2]i32 { + _ = self; + var size: usize = nums.len; + var i: usize = 0; + // 两层循环,时间复杂度 O(n^2) + while (i < size - 1) : (i += 1) { + var j = i + 1; + while (j < size) : (j += 1) { + if (nums[i] + nums[j] == target) { + return [_]i32{@intCast(i32, i), @intCast(i32, j)}; + } + } + } + return undefined; + } +}; + +const SolutionHashMap = struct { + pub fn twoSum(self: *SolutionHashMap, nums: []i32, target: i32) ![2]i32 { + _ = self; + var size: usize = nums.len; + // 辅助哈希表,空间复杂度 O(n) + var dic = std.AutoHashMap(i32, i32).init(std.heap.page_allocator); + defer dic.deinit(); + var i: usize = 0; + // 单层循环,时间复杂度 O(n) + while (i < size) : (i += 1) { + if (dic.contains(target - nums[i])) { + return [_]i32{dic.get(target - nums[i]).?, @intCast(i32, i)}; + } + try dic.put(nums[i], @intCast(i32, i)); + } + return undefined; + } +}; + +// Driver Code +pub fn main() !void { + // ======= Test Case ======= + var nums = [_]i32{ 2, 7, 11, 15 }; + var target: i32 = 9; + // 方法一 + var slt1 = SolutionBruteForce{}; + var res = slt1.twoSum(&nums, target); + std.debug.print("方法一 res = ", .{}); + inc.PrintUtil.printArray(i32, &res); + // 方法二 + var slt2 = SolutionHashMap{}; + res = try slt2.twoSum(&nums, target); + std.debug.print("方法二 res = ", .{}); + inc.PrintUtil.printArray(i32, &res); +} + 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 dacf4595..bd2a1464 100644 --- a/codes/zig/chapter_computational_complexity/worst_best_time_complexity.zig +++ b/codes/zig/chapter_computational_complexity/worst_best_time_complexity.zig @@ -3,6 +3,7 @@ // Author: sjinzh (sjinzh@gmail.com) const std = @import("std"); +const inc = @import("include"); // 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 pub fn randomNumbers(comptime n: usize) [n]i32 { @@ -26,17 +27,15 @@ pub fn findOne(nums: []i32) i32 { } // Driver Code -pub fn main() !void { +pub fn main() void { var i: i32 = 0; while (i < 10) : (i += 1) { const n: usize = 100; var nums = randomNumbers(n); var index = findOne(&nums); std.debug.print("\n数组 [ 1, 2, ..., n ] 被打乱后 = ", .{}); - for (nums) |num, j| { - std.debug.print("{}{s}", .{num, if (j == nums.len-1) "" else "," }); - } - std.debug.print("\n数字 1 的索引为 {}\n", .{index}); + inc.PrintUtil.printArray(i32, &nums); + std.debug.print("数字 1 的索引为 {}\n", .{index}); } } diff --git a/codes/zig/include/PrintUtil.zig b/codes/zig/include/PrintUtil.zig new file mode 100644 index 00000000..5adac2a6 --- /dev/null +++ b/codes/zig/include/PrintUtil.zig @@ -0,0 +1,13 @@ +// File: TreeNode.zig +// Created Time: 2023-01-07 +// Author: sjinzh (sjinzh@gmail.com) + +const std = @import("std"); + +// 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 ", " }); + } +} diff --git a/codes/zig/include/include.zig b/codes/zig/include/include.zig new file mode 100644 index 00000000..b4f34782 --- /dev/null +++ b/codes/zig/include/include.zig @@ -0,0 +1,5 @@ +// File: include.zig +// Created Time: 2023-01-04 +// Author: sjinzh (sjinzh@gmail.com) + +pub const PrintUtil = @import("PrintUtil.zig"); \ No newline at end of file