mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-24 17:17:14 +00:00
完善所以c#相关的文档和代码
This commit is contained in:
@@ -106,6 +106,19 @@ comments: true
|
||||
=== "C#"
|
||||
|
||||
```csharp title="linear_search.cs"
|
||||
/* 线性查找(数组) */
|
||||
int linearSearch(int[] nums, int target)
|
||||
{
|
||||
// 遍历数组
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
{
|
||||
// 找到目标元素,返回其索引
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
}
|
||||
// 未找到目标元素,返回 -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -209,7 +222,20 @@ comments: true
|
||||
=== "C#"
|
||||
|
||||
```csharp title="linear_search.cs"
|
||||
|
||||
/* 线性查找(链表) */
|
||||
ListNode? linearSearch(ListNode head, int target)
|
||||
{
|
||||
// 遍历链表
|
||||
while (head != null)
|
||||
{
|
||||
// 找到目标结点,返回之
|
||||
if (head.val == target)
|
||||
return head;
|
||||
head = head.next;
|
||||
}
|
||||
// 未找到目标结点,返回 null
|
||||
return null;
|
||||
}
|
||||
```
|
||||
|
||||
## 复杂度分析
|
||||
|
||||
Reference in New Issue
Block a user