Update .gitignore

Add build script for Zig.
This commit is contained in:
krahets
2023-02-09 22:57:25 +08:00
parent 3465b300e9
commit ec25970e8e
30 changed files with 226 additions and 532 deletions

View File

@@ -6,43 +6,40 @@ 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)};
}
pub fn twoSumBruteForce(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;
}
};
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));
pub fn twoSumHashTable(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)};
}
return undefined;
try dic.put(nums[i], @intCast(i32, i));
}
};
return undefined;
}
// Driver Code
pub fn main() !void {
@@ -50,14 +47,11 @@ pub fn main() !void {
var nums = [_]i32{ 2, 7, 11, 15 };
var target: i32 = 9;
// 方法一
var slt1 = SolutionBruteForce{};
var res = slt1.twoSum(&nums, target);
twoSumBruteForce(&nums, target);
std.debug.print("方法一 res = ", .{});
inc.PrintUtil.printArray(i32, &res);
// 方法二
var slt2 = SolutionHashMap{};
res = try slt2.twoSum(&nums, target);
twoSumHashTable(&nums, target);
std.debug.print("方法二 res = ", .{});
inc.PrintUtil.printArray(i32, &res);
}

View File

@@ -72,7 +72,7 @@ fn bubbleSort(nums: []i32) i32 {
}
// 指数阶(循环实现)
fn exponential(n: i32) i32{
fn exponential(n: i32) i32 {
var count: i32 = 0;
var bas: i32 = 1;
var i: i32 = 0;
@@ -89,14 +89,13 @@ fn exponential(n: i32) i32{
}
// 指数阶(递归实现)
fn expRecur(n: i32) i32{
fn expRecur(n: i32) i32 {
if (n == 1) return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}
// 对数阶(循环实现)
fn logarithmic(n: f32) i32
{
fn logarithmic(n: f32) i32 {
var count: i32 = 0;
var n_var = n;
while (n_var > 1)
@@ -108,15 +107,13 @@ fn logarithmic(n: f32) i32
}
// 对数阶(递归实现)
fn logRecur(n: f32) i32
{
fn logRecur(n: f32) i32 {
if (n <= 1) return 0;
return logRecur(n / 2) + 1;
}
// 线性对数阶
fn linearLogRecur(n: f32) i32
{
fn linearLogRecur(n: f32) i32 {
if (n <= 1) return 1;
var count: i32 = linearLogRecur(n / 2) +
linearLogRecur(n / 2);

View File

@@ -10,7 +10,7 @@ const Entry = struct {
key: usize = undefined,
val: []const u8 = undefined,
pub fn init(key: usize, val: []const u8) Entry {
pub fn init(key: usize, val: []const u8) Entry {
return Entry {
.key = key,
.val = val,

View File

@@ -13,7 +13,7 @@ fn hashingSearchArray(comptime T: type, map: std.AutoHashMap(T, T), target: T) T
return map.get(target).?;
}
// 哈希查找(数组
// 哈希查找(链表
fn hashingSearchLinkedList(comptime T: type, map: std.AutoHashMap(T, *inc.ListNode(T)), target: T) ?*inc.ListNode(T) {
// 哈希表的 key: 目标结点值value: 结点对象
// 若哈希表中无此 key ,返回 null

View File

@@ -6,7 +6,7 @@ const std = @import("std");
const inc = @import("include");
// 线性查找(数组)
fn linearSearchList(comptime T: type, nums: std.ArrayList(T), target: T) T {
fn linearSearchArray(comptime T: type, nums: std.ArrayList(T), target: T) T {
// 遍历数组
for (nums.items) |num, i| {
// 找到目标元素, 返回其索引
@@ -38,7 +38,7 @@ pub fn main() !void {
var nums = std.ArrayList(i32).init(std.heap.page_allocator);
defer nums.deinit();
try nums.appendSlice(&[_]i32{ 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 });
var index = linearSearchList(i32, nums, target);
var index = linearSearchArray(i32, nums, target);
std.debug.print("目标元素 3 的索引 = {}\n", .{index});
// 在链表中执行线性查找

View File

@@ -7,6 +7,7 @@ const inc = @import("include");
// 快速排序类
const QuickSort = struct {
// 元素交换
pub fn swap(nums: []i32, i: usize, j: usize) void {
var tmp = nums[i];
@@ -42,6 +43,7 @@ const QuickSort = struct {
// 快速排序类(中位基准数优化)
const QuickSortMedian = struct {
// 元素交换
pub fn swap(nums: []i32, i: usize, j: usize) void {
var tmp = nums[i];
@@ -95,6 +97,7 @@ const QuickSortMedian = struct {
// 快速排序类(尾递归优化)
const QuickSortTailCall = struct {
// 元素交换
pub fn swap(nums: []i32, i: usize, j: usize) void {
var tmp = nums[i];