mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-03 05:27:55 +08:00
Update linear_search and hashing_search.
This commit is contained in:
@ -6,7 +6,7 @@ const std = @import("std");
|
||||
const inc = @import("include");
|
||||
|
||||
// 哈希查找(数组)
|
||||
fn hashingSearch(comptime T: type, map: std.AutoHashMap(T, T), target: T) T {
|
||||
fn hashingSearchArray(comptime T: type, map: std.AutoHashMap(T, T), target: T) T {
|
||||
// 哈希表的 key: 目标元素,value: 索引
|
||||
// 若哈希表中无此 key ,返回 -1
|
||||
if (map.getKey(target) == null) return -1;
|
||||
@ -14,7 +14,7 @@ fn hashingSearch(comptime T: type, map: std.AutoHashMap(T, T), target: T) T {
|
||||
}
|
||||
|
||||
// 哈希查找(数组)
|
||||
fn hashingSearch1(comptime T: type, map: std.AutoHashMap(T, *inc.ListNode(T)), target: T) ?*inc.ListNode(T) {
|
||||
fn hashingSearchLinkedList(comptime T: type, map: std.AutoHashMap(T, *inc.ListNode(T)), target: T) ?*inc.ListNode(T) {
|
||||
// 哈希表的 key: 目标结点值,value: 结点对象
|
||||
// 若哈希表中无此 key ,返回 null
|
||||
if (map.getKey(target) == null) return null;
|
||||
@ -33,7 +33,7 @@ pub fn main() !void {
|
||||
for (nums) |num, i| {
|
||||
try map.put(num, @intCast(i32, i)); // key: 元素,value: 索引
|
||||
}
|
||||
var index = hashingSearch(i32, map, target);
|
||||
var index = hashingSearchArray(i32, map, target);
|
||||
std.debug.print("目标元素 3 的索引 = {}\n", .{index});
|
||||
|
||||
// 哈希查找(链表)
|
||||
@ -48,7 +48,7 @@ pub fn main() !void {
|
||||
try map1.put(head.?.val, head.?);
|
||||
head = head.?.next;
|
||||
}
|
||||
var node = hashingSearch1(i32, map1, target);
|
||||
var node = hashingSearchLinkedList(i32, map1, target);
|
||||
std.debug.print("目标结点值 3 的对应结点对象为 ", .{});
|
||||
try inc.PrintUtil.printLinkedList(i32, node);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user