This commit is contained in:
krahets
2023-02-09 22:55:29 +08:00
parent 3dcf65c61b
commit 7d86e2dd11
18 changed files with 1029 additions and 71 deletions
+23 -2
View File
@@ -151,7 +151,18 @@ comments: true
=== "Zig"
```zig title="linear_search.zig"
// 线性查找(数组)
fn linearSearchArray(comptime T: type, nums: std.ArrayList(T), target: T) T {
// 遍历数组
for (nums.items) |num, i| {
// 找到目标元素, 返回其索引
if (num == target) {
return @intCast(T, i);
}
}
// 未找到目标元素,返回 -1
return -1;
}
```
再比如,我们想要在给定一个目标结点值 `target` ,返回此结点对象,也可以在链表中进行线性查找。
@@ -304,7 +315,17 @@ comments: true
=== "Zig"
```zig title="linear_search.zig"
// 线性查找(链表)
fn linearSearchLinkedList(comptime T: type, node: ?*inc.ListNode(T), target: T) ?*inc.ListNode(T) {
var head = node;
// 遍历链表
while (head != null) {
// 找到目标结点,返回之
if (head.?.val == target) return head;
head = head.?.next;
}
return null;
}
```
## 10.1.2. 复杂度分析