mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-07 23:57:02 +08:00
Bug fixes and improvements (#1813)
* Sync zh and zh-hant version. * Add the Warp sponsor banner. * Update README with acknowledgments and Warp recommendation Added acknowledgments and a recommendation for the Warp terminal application. * Update README.md * Update links in README.md to use HTTPS * Sync zh and zh-hant versions. * Add special thanks for Warp spnsorship. * Use official warp image link.
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
// File: iteration.zig
|
||||
// Created Time: 2023-09-27
|
||||
// Author: QiLOL (pikaqqpika@gmail.com)
|
||||
// Author: QiLOL (pikaqqpika@gmail.com), CreatorMetaSky (creator_meta_sky@163.com)
|
||||
|
||||
const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
@ -9,20 +9,19 @@ const Allocator = std.mem.Allocator;
|
||||
fn forLoop(n: usize) i32 {
|
||||
var res: i32 = 0;
|
||||
// 迴圈求和 1, 2, ..., n-1, n
|
||||
for (1..n+1) |i| {
|
||||
res = res + @as(i32, @intCast(i));
|
||||
for (1..n + 1) |i| {
|
||||
res += @intCast(i);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
// while 迴圈
|
||||
fn whileLoop(n: i32) i32 {
|
||||
var res: i32 = 0;
|
||||
var i: i32 = 1; // 初始化條件變數
|
||||
// 迴圈求和 1, 2, ..., n-1, n
|
||||
while (i <= n) {
|
||||
while (i <= n) : (i += 1) {
|
||||
res += @intCast(i);
|
||||
i += 1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@ -32,11 +31,12 @@ fn whileLoopII(n: i32) i32 {
|
||||
var res: i32 = 0;
|
||||
var i: i32 = 1; // 初始化條件變數
|
||||
// 迴圈求和 1, 4, 10, ...
|
||||
while (i <= n) {
|
||||
res += @intCast(i);
|
||||
while (i <= n) : ({
|
||||
// 更新條件變數
|
||||
i += 1;
|
||||
i *= 2;
|
||||
}) {
|
||||
res += @intCast(i);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@ -47,31 +47,45 @@ fn nestedForLoop(allocator: Allocator, n: usize) ![]const u8 {
|
||||
defer res.deinit();
|
||||
var buffer: [20]u8 = undefined;
|
||||
// 迴圈 i = 1, 2, ..., n-1, n
|
||||
for (1..n+1) |i| {
|
||||
for (1..n + 1) |i| {
|
||||
// 迴圈 j = 1, 2, ..., n-1, n
|
||||
for (1..n+1) |j| {
|
||||
var _str = try std.fmt.bufPrint(&buffer, "({d}, {d}), ", .{i, j});
|
||||
try res.appendSlice(_str);
|
||||
for (1..n + 1) |j| {
|
||||
const str = try std.fmt.bufPrint(&buffer, "({d}, {d}), ", .{ i, j });
|
||||
try res.appendSlice(str);
|
||||
}
|
||||
}
|
||||
return res.toOwnedSlice();
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
pub fn main() !void {
|
||||
pub fn run() !void {
|
||||
var gpa = std.heap.DebugAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
const n: i32 = 5;
|
||||
var res: i32 = 0;
|
||||
|
||||
res = forLoop(n);
|
||||
std.debug.print("\nfor 迴圈的求和結果 res = {}\n", .{res});
|
||||
std.debug.print("for 迴圈的求和結果 res = {}\n", .{res});
|
||||
|
||||
res = whileLoop(n);
|
||||
std.debug.print("\nwhile 迴圈的求和結果 res = {}\n", .{res});
|
||||
std.debug.print("while 迴圈的求和結果 res = {}\n", .{res});
|
||||
|
||||
res = whileLoopII(n);
|
||||
std.debug.print("\nwhile 迴圈(兩次更新)求和結果 res = {}\n", .{res});
|
||||
std.debug.print("while 迴圈(兩次更新)求和結果 res = {}\n", .{res});
|
||||
|
||||
const allocator = std.heap.page_allocator;
|
||||
const resStr = try nestedForLoop(allocator, n);
|
||||
std.debug.print("\n雙層 for 迴圈的走訪結果 {s}\n", .{resStr});
|
||||
std.debug.print("雙層 for 迴圈的走訪結果 {s}\n", .{resStr});
|
||||
allocator.free(resStr);
|
||||
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
try run();
|
||||
}
|
||||
|
||||
test "interation" {
|
||||
try run();
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
// File: recursion.zig
|
||||
// Created Time: 2023-09-27
|
||||
// Author: QiLOL (pikaqqpika@gmail.com)
|
||||
|
||||
// Author: QiLOL (pikaqqpika@gmail.com), CreatorMetaSky (creator_meta_sky@163.com)
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
// 遞迴函式
|
||||
@ -11,7 +11,7 @@ fn recur(n: i32) i32 {
|
||||
return 1;
|
||||
}
|
||||
// 遞:遞迴呼叫
|
||||
var res: i32 = recur(n - 1);
|
||||
const res = recur(n - 1);
|
||||
// 迴:返回結果
|
||||
return n + res;
|
||||
}
|
||||
@ -54,25 +54,35 @@ fn fib(n: i32) i32 {
|
||||
return n - 1;
|
||||
}
|
||||
// 遞迴呼叫 f(n) = f(n-1) + f(n-2)
|
||||
var res: i32 = fib(n - 1) + fib(n - 2);
|
||||
const res: i32 = fib(n - 1) + fib(n - 2);
|
||||
// 返回結果 f(n)
|
||||
return res;
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
pub fn main() !void {
|
||||
pub fn run() void {
|
||||
const n: i32 = 5;
|
||||
var res: i32 = 0;
|
||||
|
||||
res = recur(n);
|
||||
std.debug.print("\n遞迴函式的求和結果 res = {}\n", .{recur(n)});
|
||||
std.debug.print("遞迴函式的求和結果 res = {}\n", .{recur(n)});
|
||||
|
||||
res = forLoopRecur(n);
|
||||
std.debug.print("\n使用迭代模擬遞迴的求和結果 res = {}\n", .{forLoopRecur(n)});
|
||||
std.debug.print("使用迭代模擬遞迴的求和結果 res = {}\n", .{forLoopRecur(n)});
|
||||
|
||||
res = tailRecur(n, 0);
|
||||
std.debug.print("\n尾遞迴函式的求和結果 res = {}\n", .{tailRecur(n, 0)});
|
||||
std.debug.print("尾遞迴函式的求和結果 res = {}\n", .{tailRecur(n, 0)});
|
||||
|
||||
res = fib(n);
|
||||
std.debug.print("\n費波那契數列的第 {} 項為 {}\n", .{n, fib(n)});
|
||||
std.debug.print("費波那契數列的第 {} 項為 {}\n", .{ n, fib(n) });
|
||||
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
||||
pub fn main() void {
|
||||
run();
|
||||
}
|
||||
|
||||
test "recursion" {
|
||||
run();
|
||||
}
|
||||
|
||||
@ -1,9 +1,11 @@
|
||||
// File: space_complexity.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");
|
||||
const ListNode = utils.ListNode;
|
||||
const TreeNode = utils.TreeNode;
|
||||
|
||||
// 函式
|
||||
fn function() i32 {
|
||||
@ -15,13 +17,13 @@ fn function() i32 {
|
||||
fn constant(n: i32) void {
|
||||
// 常數、變數、物件佔用 O(1) 空間
|
||||
const a: i32 = 0;
|
||||
var b: i32 = 0;
|
||||
var nums = [_]i32{0}**10000;
|
||||
var node = inc.ListNode(i32){.val = 0};
|
||||
const b: i32 = 0;
|
||||
const nums = [_]i32{0} ** 10000;
|
||||
const node = ListNode(i32){ .val = 0 };
|
||||
var i: i32 = 0;
|
||||
// 迴圈中的變數佔用 O(1) 空間
|
||||
while (i < n) : (i += 1) {
|
||||
var c: i32 = 0;
|
||||
const c: i32 = 0;
|
||||
_ = c;
|
||||
}
|
||||
// 迴圈中的函式佔用 O(1) 空間
|
||||
@ -38,7 +40,7 @@ fn constant(n: i32) void {
|
||||
// 線性階
|
||||
fn linear(comptime n: i32) !void {
|
||||
// 長度為 n 的陣列佔用 O(n) 空間
|
||||
var nums = [_]i32{0}**n;
|
||||
const nums = [_]i32{0} ** n;
|
||||
// 長度為 n 的串列佔用 O(n) 空間
|
||||
var nodes = std.ArrayList(i32).init(std.heap.page_allocator);
|
||||
defer nodes.deinit();
|
||||
@ -85,23 +87,35 @@ fn quadratic(n: i32) !void {
|
||||
// 平方階(遞迴實現)
|
||||
fn quadraticRecur(comptime n: i32) i32 {
|
||||
if (n <= 0) return 0;
|
||||
var nums = [_]i32{0}**n;
|
||||
std.debug.print("遞迴 n = {} 中的 nums 長度 = {}\n", .{n, nums.len});
|
||||
const nums = [_]i32{0} ** n;
|
||||
std.debug.print("遞迴 n = {} 中的 nums 長度 = {}\n", .{ n, nums.len });
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
// 指數階(建立滿二元樹)
|
||||
fn buildTree(mem_allocator: std.mem.Allocator, n: i32) !?*inc.TreeNode(i32) {
|
||||
fn buildTree(allocator: std.mem.Allocator, n: i32) !?*TreeNode(i32) {
|
||||
if (n == 0) return null;
|
||||
const root = try mem_allocator.create(inc.TreeNode(i32));
|
||||
const root = try allocator.create(TreeNode(i32));
|
||||
root.init(0);
|
||||
root.left = try buildTree(mem_allocator, n - 1);
|
||||
root.right = try buildTree(mem_allocator, n - 1);
|
||||
root.left = try buildTree(allocator, n - 1);
|
||||
root.right = try buildTree(allocator, n - 1);
|
||||
return root;
|
||||
}
|
||||
|
||||
// 釋放樹的記憶體
|
||||
fn freeTree(allocator: std.mem.Allocator, root: ?*const TreeNode(i32)) void {
|
||||
if (root == null) return;
|
||||
freeTree(allocator, root.?.left);
|
||||
freeTree(allocator, root.?.right);
|
||||
allocator.destroy(root.?);
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
pub fn main() !void {
|
||||
pub fn run() !void {
|
||||
var gpa = std.heap.DebugAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
const n: i32 = 5;
|
||||
// 常數階
|
||||
constant(n);
|
||||
@ -112,13 +126,17 @@ pub fn main() !void {
|
||||
try quadratic(n);
|
||||
_ = quadraticRecur(n);
|
||||
// 指數階
|
||||
var mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
defer mem_arena.deinit();
|
||||
var root = blk_root: {
|
||||
const mem_allocator = mem_arena.allocator();
|
||||
break :blk_root try buildTree(mem_allocator, n);
|
||||
};
|
||||
try inc.PrintUtil.printTree(root, null, false);
|
||||
const root = try buildTree(allocator, n);
|
||||
defer freeTree(allocator, root);
|
||||
std.debug.print("{}\n", .{utils.fmt.tree(i32, root)});
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
}
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
try run();
|
||||
}
|
||||
|
||||
test "space_complexity" {
|
||||
try run();
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
// File: time_complexity.zig
|
||||
// Created Time: 2022-12-28
|
||||
// Author: codingonion (coderonion@gmail.com)
|
||||
// Author: codingonion (coderonion@gmail.com), CreatorMetaSky (creator_meta_sky@163.com)
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
@ -10,7 +10,7 @@ fn constant(n: i32) i32 {
|
||||
var count: i32 = 0;
|
||||
const size: i32 = 100_000;
|
||||
var i: i32 = 0;
|
||||
while(i<size) : (i += 1) {
|
||||
while (i < size) : (i += 1) {
|
||||
count += 1;
|
||||
}
|
||||
return count;
|
||||
@ -52,7 +52,7 @@ fn quadratic(n: i32) i32 {
|
||||
|
||||
// 平方階(泡沫排序)
|
||||
fn bubbleSort(nums: []i32) i32 {
|
||||
var count: i32 = 0; // 計數器
|
||||
var count: i32 = 0; // 計數器
|
||||
// 外迴圈:未排序區間為 [0, i]
|
||||
var i: i32 = @as(i32, @intCast(nums.len)) - 1;
|
||||
while (i > 0) : (i -= 1) {
|
||||
@ -61,10 +61,10 @@ fn bubbleSort(nums: []i32) i32 {
|
||||
while (j < i) : (j += 1) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// 交換 nums[j] 與 nums[j + 1]
|
||||
var tmp = nums[j];
|
||||
const tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
count += 3; // 元素交換包含 3 個單元操作
|
||||
count += 3; // 元素交換包含 3 個單元操作
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -97,11 +97,9 @@ fn expRecur(n: i32) i32 {
|
||||
// 對數階(迴圈實現)
|
||||
fn logarithmic(n: i32) i32 {
|
||||
var count: i32 = 0;
|
||||
var n_var = n;
|
||||
while (n_var > 1)
|
||||
{
|
||||
n_var = n_var / 2;
|
||||
count +=1;
|
||||
var n_var: i32 = n;
|
||||
while (n_var > 1) : (n_var = @divTrunc(n_var, 2)) {
|
||||
count += 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
@ -109,13 +107,13 @@ fn logarithmic(n: i32) i32 {
|
||||
// 對數階(遞迴實現)
|
||||
fn logRecur(n: i32) i32 {
|
||||
if (n <= 1) return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
return logRecur(@divTrunc(n, 2)) + 1;
|
||||
}
|
||||
|
||||
// 線性對數階
|
||||
fn linearLogRecur(n: i32) i32 {
|
||||
if (n <= 1) return 1;
|
||||
var count: i32 = linearLogRecur(n / 2) + linearLogRecur(n / 2);
|
||||
var count: i32 = linearLogRecur(@divTrunc(n, 2)) + linearLogRecur(@divTrunc(n, 2));
|
||||
var i: i32 = 0;
|
||||
while (i < n) : (i += 1) {
|
||||
count += 1;
|
||||
@ -136,7 +134,7 @@ fn factorialRecur(n: i32) i32 {
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
pub fn main() !void {
|
||||
pub fn run() void {
|
||||
// 可以修改 n 執行,體會一下各種複雜度的操作數量變化趨勢
|
||||
const n: i32 = 8;
|
||||
std.debug.print("輸入資料大小 n = {}\n", .{n});
|
||||
@ -146,14 +144,14 @@ pub fn main() !void {
|
||||
|
||||
count = linear(n);
|
||||
std.debug.print("線性階的操作數量 = {}\n", .{count});
|
||||
var nums = [_]i32{0}**n;
|
||||
var nums = [_]i32{0} ** n;
|
||||
count = arrayTraversal(&nums);
|
||||
std.debug.print("線性階(走訪陣列)的操作數量 = {}\n", .{count});
|
||||
|
||||
count = quadratic(n);
|
||||
std.debug.print("平方階的操作數量 = {}\n", .{count});
|
||||
for (&nums, 0..) |*num, i| {
|
||||
num.* = n - @as(i32, @intCast(i)); // [n,n-1,...,2,1]
|
||||
num.* = n - @as(i32, @intCast(i)); // [n,n-1,...,2,1]
|
||||
}
|
||||
count = bubbleSort(&nums);
|
||||
std.debug.print("平方階(泡沫排序)的操作數量 = {}\n", .{count});
|
||||
@ -174,6 +172,13 @@ pub fn main() !void {
|
||||
count = factorialRecur(n);
|
||||
std.debug.print("階乘階(遞迴實現)的操作數量 = {}\n", .{count});
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
run();
|
||||
}
|
||||
|
||||
test "time_complexity" {
|
||||
run();
|
||||
}
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
// File: worst_best_time_complexity.zig
|
||||
// Created Time: 2022-12-28
|
||||
// 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");
|
||||
|
||||
// 生成一個陣列,元素為 { 1, 2, ..., n },順序被打亂
|
||||
pub fn randomNumbers(comptime n: usize) [n]i32 {
|
||||
@ -29,17 +29,25 @@ pub fn findOne(nums: []i32) i32 {
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
pub fn main() !void {
|
||||
pub fn run() 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 ] 被打亂後 = ", .{});
|
||||
inc.PrintUtil.printArray(i32, &nums);
|
||||
const index = findOne(&nums);
|
||||
std.debug.print("陣列 [ 1, 2, ..., n ] 被打亂後 = ", .{});
|
||||
std.debug.print("{}\n", .{utils.fmt.slice(nums)});
|
||||
|
||||
std.debug.print("數字 1 的索引為 {}\n", .{index});
|
||||
}
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
run();
|
||||
}
|
||||
|
||||
test "worst_best_time_complexity" {
|
||||
run();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user