Skip to content

Commit

Permalink
Supplement linear_search documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
msk397 committed Dec 17, 2022
1 parent e0721dd commit 2305b09
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
7 changes: 4 additions & 3 deletions codes/go/chapter_searching/linear_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@ import (
. "github.com/krahets/hello-algo/pkg"
)

// linerSearchArray 线性查找(数组)
/* 线性查找(数组) */
func linerSearchArray(nums []int, target int) int {
// 遍历数组
for i := 0; i < len(nums); i++ {
// 找到目标元素,返回其索引
if nums[i] == target {
// 找到目标元素,返回其索引
return i
}
}
// 未找到目标元素,返回 -1
return -1
}

// linerSearchLinkedList 线性查找(链表)
/* 线性查找(链表) */
func linerSearchLinkedList(node *ListNode, target int) *ListNode {
// 遍历链表
for node != nil {
// 找到目标元素,返回其索引
if node.Val == target {
return node
}
Expand Down
27 changes: 25 additions & 2 deletions docs/chapter_searching/linear_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,18 @@ comments: true
=== "Go"

```go title="linear_search.go"

/* 线性查找(数组) */
func linerSearchArray(nums []int, target int) int {
// 遍历数组
for i := 0; i < len(nums); i++ {
// 找到目标元素,返回其索引
if nums[i] == target {
return i
}
}
// 未找到目标元素,返回 -1
return -1
}
```

=== "JavaScript"
Expand Down Expand Up @@ -138,7 +149,19 @@ comments: true
=== "Go"

```go title="linear_search.go"

/* 线性查找(链表)*/
func linerSearchLinkedList(node *ListNode, target int) *ListNode {
// 遍历链表
for node != nil {
// 找到目标结点,返回之
if node.Val == target {
return node
}
node = node.Next
}
// 未找到目标元素,返回 nil
return nil
}
```

=== "JavaScript"
Expand Down

0 comments on commit 2305b09

Please sign in to comment.