mirror of
https://github.com/krahets/hello-algo.git
synced 2025-02-02 22:43:50 +08:00
Update the coding style for Zig (#336)
* Update the coding style for Zig * Update array.rs
This commit is contained in:
parent
cb73007495
commit
063501068b
@ -4,10 +4,12 @@
|
|||||||
* Author: xBLACICEx (xBLACKICEx@outlook.com), sjinzh (sjinzh@gmail.com)
|
* Author: xBLACICEx (xBLACKICEx@outlook.com), sjinzh (sjinzh@gmail.com)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use rand::Rng;
|
||||||
|
|
||||||
/* 随机返回一个数组元素 */
|
/* 随机返回一个数组元素 */
|
||||||
fn random_access(nums: &[i32]) -> i32 {
|
fn random_access(nums: &[i32]) -> i32 {
|
||||||
// 在区间 [0, nums.len()) 中随机抽取一个数字
|
// 在区间 [0, nums.len()) 中随机抽取一个数字
|
||||||
let random_index = rand::random::<usize>() % nums.len();
|
let random_index = rand::thread_rng().gen_range(0..nums.len());
|
||||||
// 获取并返回随机元素
|
// 获取并返回随机元素
|
||||||
let random_num = nums[random_index];
|
let random_num = nums[random_index];
|
||||||
random_num
|
random_num
|
||||||
|
@ -112,7 +112,6 @@ pub fn main() !void {
|
|||||||
var index = find(nums, 3);
|
var index = find(nums, 3);
|
||||||
std.debug.print("\n在 nums 中查找元素 3 ,得到索引 = {}\n", .{index});
|
std.debug.print("\n在 nums 中查找元素 3 ,得到索引 = {}\n", .{index});
|
||||||
|
|
||||||
const getchar = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
_ = getchar;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,6 +80,5 @@ pub fn main() !void {
|
|||||||
var index = find(&n0, 2);
|
var index = find(&n0, 2);
|
||||||
std.debug.print("链表中值为 2 的结点的索引 = {}\n", .{index});
|
std.debug.print("链表中值为 2 的结点的索引 = {}\n", .{index});
|
||||||
|
|
||||||
const getchar = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
_ = getchar;
|
|
||||||
}
|
}
|
@ -74,7 +74,6 @@ pub fn main() !void {
|
|||||||
std.debug.print("\n排序列表后 list = ", .{});
|
std.debug.print("\n排序列表后 list = ", .{});
|
||||||
inc.PrintUtil.printList(i32, list);
|
inc.PrintUtil.printList(i32, list);
|
||||||
|
|
||||||
const getchar = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
_ = getchar;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@ const std = @import("std");
|
|||||||
const inc = @import("include");
|
const inc = @import("include");
|
||||||
|
|
||||||
// 列表类简易实现
|
// 列表类简易实现
|
||||||
// 编译期泛型
|
|
||||||
pub fn MyList(comptime T: type) type {
|
pub fn MyList(comptime T: type) type {
|
||||||
return struct {
|
return struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
@ -171,7 +170,6 @@ pub fn main() !void {
|
|||||||
inc.PrintUtil.printArray(i32, try list.toArray());
|
inc.PrintUtil.printArray(i32, try list.toArray());
|
||||||
std.debug.print(" ,容量 = {} ,长度 = {}\n", .{list.capacity(), list.size()});
|
std.debug.print(" ,容量 = {} ,长度 = {}\n", .{list.capacity(), list.size()});
|
||||||
|
|
||||||
const getchar = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
_ = getchar;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,6 +120,5 @@ pub fn main() !void {
|
|||||||
};
|
};
|
||||||
try inc.PrintUtil.printTree(root, null, false);
|
try inc.PrintUtil.printTree(root, null, false);
|
||||||
|
|
||||||
const getchar = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
_ = getchar;
|
|
||||||
}
|
}
|
@ -158,6 +158,5 @@ pub fn main() !void {
|
|||||||
}
|
}
|
||||||
value_set.deinit();
|
value_set.deinit();
|
||||||
|
|
||||||
const getchar = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
_ = getchar;
|
|
||||||
}
|
}
|
@ -49,7 +49,6 @@ pub fn main() !void {
|
|||||||
std.debug.print("{s}\n", .{kv.value_ptr.*});
|
std.debug.print("{s}\n", .{kv.value_ptr.*});
|
||||||
}
|
}
|
||||||
|
|
||||||
const getchar = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
_ = getchar;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,6 +76,5 @@ pub fn main() !void {
|
|||||||
std.debug.print("\n输入列表并建立小顶堆后\n", .{});
|
std.debug.print("\n输入列表并建立小顶堆后\n", .{});
|
||||||
try inc.PrintUtil.printHeap(i32, mem_allocator, minHeap);
|
try inc.PrintUtil.printHeap(i32, mem_allocator, minHeap);
|
||||||
|
|
||||||
const getchar = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
_ = getchar;
|
|
||||||
}
|
}
|
@ -6,7 +6,6 @@ const std = @import("std");
|
|||||||
const inc = @import("include");
|
const inc = @import("include");
|
||||||
|
|
||||||
// 堆类简易实现
|
// 堆类简易实现
|
||||||
// 编译期泛型
|
|
||||||
pub fn MaxHeap(comptime T: type) type {
|
pub fn MaxHeap(comptime T: type) type {
|
||||||
return struct {
|
return struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
@ -181,10 +180,9 @@ pub fn main() !void {
|
|||||||
std.debug.print("\n堆元素数量为 {}", .{size});
|
std.debug.print("\n堆元素数量为 {}", .{size});
|
||||||
|
|
||||||
// 判断堆是否为空
|
// 判断堆是否为空
|
||||||
var isEmpty = maxHeap.isEmpty();
|
var is_empty = maxHeap.isEmpty();
|
||||||
std.debug.print("\n堆是否为空 {}\n", .{isEmpty});
|
std.debug.print("\n堆是否为空 {}\n", .{is_empty});
|
||||||
|
|
||||||
const getchar = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
_ = getchar;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,7 +50,6 @@ pub fn main() !void {
|
|||||||
std.debug.print("目标结点值 3 的对应结点对象为 ", .{});
|
std.debug.print("目标结点值 3 的对应结点对象为 ", .{});
|
||||||
try inc.PrintUtil.printLinkedList(i32, node);
|
try inc.PrintUtil.printLinkedList(i32, node);
|
||||||
|
|
||||||
const getchar = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
_ = getchar;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,7 +56,6 @@ pub fn main() !void {
|
|||||||
std.debug.print("\n冒泡排序完成后 nums1 = ", .{});
|
std.debug.print("\n冒泡排序完成后 nums1 = ", .{});
|
||||||
inc.PrintUtil.printArray(i32, &nums1);
|
inc.PrintUtil.printArray(i32, &nums1);
|
||||||
|
|
||||||
const getchar = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
_ = getchar;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +27,6 @@ pub fn main() !void {
|
|||||||
std.debug.print("插入排序完成后 nums = ", .{});
|
std.debug.print("插入排序完成后 nums = ", .{});
|
||||||
inc.PrintUtil.printArray(i32, &nums);
|
inc.PrintUtil.printArray(i32, &nums);
|
||||||
|
|
||||||
const getchar = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
_ = getchar;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,8 +124,8 @@ pub fn main() !void {
|
|||||||
std.debug.print("\n队列长度 size = {}", .{size});
|
std.debug.print("\n队列长度 size = {}", .{size});
|
||||||
|
|
||||||
// 判断队列是否为空
|
// 判断队列是否为空
|
||||||
var isEmpty = queue.isEmpty();
|
var is_empty = queue.isEmpty();
|
||||||
std.debug.print("\n队列是否为空 = {}", .{isEmpty});
|
std.debug.print("\n队列是否为空 = {}", .{is_empty});
|
||||||
|
|
||||||
// 测试环形数组
|
// 测试环形数组
|
||||||
var i: i32 = 0;
|
var i: i32 = 0;
|
||||||
|
@ -6,7 +6,6 @@ const std = @import("std");
|
|||||||
const inc = @import("include");
|
const inc = @import("include");
|
||||||
|
|
||||||
// 基于数组实现的栈
|
// 基于数组实现的栈
|
||||||
// 编译期泛型
|
|
||||||
pub fn ArrayStack(comptime T: type) type {
|
pub fn ArrayStack(comptime T: type) type {
|
||||||
return struct {
|
return struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
@ -78,11 +77,11 @@ pub fn main() !void {
|
|||||||
inc.PrintUtil.printList(i32, stack.toList());
|
inc.PrintUtil.printList(i32, stack.toList());
|
||||||
|
|
||||||
// 访问栈顶元素
|
// 访问栈顶元素
|
||||||
var top = stack.top();
|
var peek = stack.top();
|
||||||
std.debug.print("\n栈顶元素 top = {}", .{top});
|
std.debug.print("\n栈顶元素 peek = {}", .{peek});
|
||||||
|
|
||||||
// 元素出栈
|
// 元素出栈
|
||||||
top = stack.pop();
|
var top = stack.pop();
|
||||||
std.debug.print("\n出栈元素 pop = {},出栈后 stack = ", .{top});
|
std.debug.print("\n出栈元素 pop = {},出栈后 stack = ", .{top});
|
||||||
inc.PrintUtil.printList(i32, stack.toList());
|
inc.PrintUtil.printList(i32, stack.toList());
|
||||||
|
|
||||||
@ -91,9 +90,8 @@ pub fn main() !void {
|
|||||||
std.debug.print("\n栈的长度 size = {}", .{size});
|
std.debug.print("\n栈的长度 size = {}", .{size});
|
||||||
|
|
||||||
// 判断栈是否为空
|
// 判断栈是否为空
|
||||||
var empty = stack.empty();
|
var is_empty = stack.empty();
|
||||||
std.debug.print("\n栈是否为空 = {}", .{empty});
|
std.debug.print("\n栈是否为空 = {}", .{is_empty});
|
||||||
|
|
||||||
const getchar = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
_ = getchar;
|
|
||||||
}
|
}
|
||||||
|
@ -44,8 +44,8 @@ pub fn main() !void {
|
|||||||
std.debug.print("\n双向队列长度 size = {}", .{size});
|
std.debug.print("\n双向队列长度 size = {}", .{size});
|
||||||
|
|
||||||
// 判断双向队列是否为空
|
// 判断双向队列是否为空
|
||||||
var isEmpty = if (deque.len == 0) true else false;
|
var is_empty = if (deque.len == 0) true else false;
|
||||||
std.debug.print("\n双向队列是否为空 = {}", .{isEmpty});
|
std.debug.print("\n双向队列是否为空 = {}", .{is_empty});
|
||||||
|
|
||||||
_ = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
}
|
}
|
||||||
|
@ -222,8 +222,8 @@ pub fn main() !void {
|
|||||||
std.debug.print("\n队列长度 size = {}", .{size});
|
std.debug.print("\n队列长度 size = {}", .{size});
|
||||||
|
|
||||||
// 判断双向队列是否为空
|
// 判断双向队列是否为空
|
||||||
var isEmpty = deque.isEmpty();
|
var is_empty = deque.isEmpty();
|
||||||
std.debug.print("\n双向队列是否为空 = {}", .{isEmpty});
|
std.debug.print("\n双向队列是否为空 = {}", .{is_empty});
|
||||||
|
|
||||||
_ = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
}
|
}
|
||||||
|
@ -120,8 +120,8 @@ pub fn main() !void {
|
|||||||
std.debug.print("\n队列长度 size = {}", .{size});
|
std.debug.print("\n队列长度 size = {}", .{size});
|
||||||
|
|
||||||
// 判断队列是否为空
|
// 判断队列是否为空
|
||||||
var isEmpty = queue.isEmpty();
|
var is_empty = queue.isEmpty();
|
||||||
std.debug.print("\n队列是否为空 = {}", .{isEmpty});
|
std.debug.print("\n队列是否为空 = {}", .{is_empty});
|
||||||
|
|
||||||
_ = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ const std = @import("std");
|
|||||||
const inc = @import("include");
|
const inc = @import("include");
|
||||||
|
|
||||||
// 基于链表实现的栈
|
// 基于链表实现的栈
|
||||||
// 编译期泛型
|
|
||||||
pub fn LinkedListStack(comptime T: type) type {
|
pub fn LinkedListStack(comptime T: type) type {
|
||||||
return struct {
|
return struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
@ -111,10 +110,9 @@ pub fn main() !void {
|
|||||||
std.debug.print("\n栈的长度 size = {}", .{size});
|
std.debug.print("\n栈的长度 size = {}", .{size});
|
||||||
|
|
||||||
// 判断栈是否为空
|
// 判断栈是否为空
|
||||||
var empty = stack.empty();
|
var is_empty = stack.empty();
|
||||||
std.debug.print("\n栈是否为空 = {}", .{empty});
|
std.debug.print("\n栈是否为空 = {}", .{is_empty});
|
||||||
|
|
||||||
const getchar = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
_ = getchar;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,8 +39,8 @@ pub fn main() !void {
|
|||||||
std.debug.print("\n队列长度 size = {}", .{size});
|
std.debug.print("\n队列长度 size = {}", .{size});
|
||||||
|
|
||||||
// 判断队列是否为空
|
// 判断队列是否为空
|
||||||
var empty = if (queue.len == 0) true else false;
|
var is_empty = if (queue.len == 0) true else false;
|
||||||
std.debug.print("\n队列是否为空 = {}", .{empty});
|
std.debug.print("\n队列是否为空 = {}", .{is_empty});
|
||||||
|
|
||||||
_ = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,5 @@ pub fn main() !void {
|
|||||||
var empty = if (stack.items.len == 0) true else false;
|
var empty = if (stack.items.len == 0) true else false;
|
||||||
std.debug.print("\n栈是否为空 = {}", .{empty});
|
std.debug.print("\n栈是否为空 = {}", .{empty});
|
||||||
|
|
||||||
const getchar = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
_ = getchar;
|
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,6 @@ pub fn main() !void {
|
|||||||
std.debug.print("删除结点 P 后\n", .{});
|
std.debug.print("删除结点 P 后\n", .{});
|
||||||
try inc.PrintUtil.printTree(&n1, null, false);
|
try inc.PrintUtil.printTree(&n1, null, false);
|
||||||
|
|
||||||
const getchar = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
_ = getchar;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,6 +53,5 @@ pub fn main() !void {
|
|||||||
std.debug.print("\n层序遍历的结点打印序列 = ", .{});
|
std.debug.print("\n层序遍历的结点打印序列 = ", .{});
|
||||||
inc.PrintUtil.printList(i32, list);
|
inc.PrintUtil.printList(i32, list);
|
||||||
|
|
||||||
const getchar = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
_ = getchar;
|
|
||||||
}
|
}
|
@ -5,7 +5,6 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
// Definition for a singly-linked list node
|
// Definition for a singly-linked list node
|
||||||
// 编译期泛型
|
|
||||||
pub fn ListNode(comptime T: type) type {
|
pub fn ListNode(comptime T: type) type {
|
||||||
return struct {
|
return struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
@ -60,7 +60,7 @@ pub fn printQueue(comptime T: type, queue: std.TailQueue(T)) void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print a HashMap
|
// Print a hash map
|
||||||
pub fn printHashMap(comptime TKey: type, comptime TValue: type, map: std.AutoHashMap(TKey, TValue)) void {
|
pub fn printHashMap(comptime TKey: type, comptime TValue: type, map: std.AutoHashMap(TKey, TValue)) void {
|
||||||
var it = map.iterator();
|
var it = map.iterator();
|
||||||
while (it.next()) |kv| {
|
while (it.next()) |kv| {
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
// Definition for a binary tree node
|
// Definition for a binary tree node
|
||||||
// 编译期泛型
|
|
||||||
pub fn TreeNode(comptime T: type) type {
|
pub fn TreeNode(comptime T: type) type {
|
||||||
return struct {
|
return struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
Loading…
Reference in New Issue
Block a user