mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-21 15:57:14 +00:00
build
This commit is contained in:
@@ -122,12 +122,12 @@ $$
|
||||
i, j := 0, len(nums)-1
|
||||
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
|
||||
for i <= j {
|
||||
m := (i + j) / 2 // 计算中点索引 m
|
||||
if nums[m] < target { // 此情况说明 target 在区间 [m+1, j] 中
|
||||
m := (i + j) / 2 // 计算中点索引 m
|
||||
if nums[m] < target { // 此情况说明 target 在区间 [m+1, j] 中
|
||||
i = m + 1
|
||||
} else if nums[m] > target { // 此情况说明 target 在区间 [i, m-1] 中
|
||||
} else if nums[m] > target { // 此情况说明 target 在区间 [i, m-1] 中
|
||||
j = m - 1
|
||||
} else { // 找到目标元素,返回其索引
|
||||
} else { // 找到目标元素,返回其索引
|
||||
return m
|
||||
}
|
||||
}
|
||||
@@ -316,12 +316,12 @@ $$
|
||||
i, j := 0, len(nums)
|
||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||
for i < j {
|
||||
m := (i + j) / 2 // 计算中点索引 m
|
||||
if nums[m] < target { // 此情况说明 target 在区间 [m+1, j) 中
|
||||
m := (i + j) / 2 // 计算中点索引 m
|
||||
if nums[m] < target { // 此情况说明 target 在区间 [m+1, j) 中
|
||||
i = m + 1
|
||||
} else if nums[m] > target { // 此情况说明 target 在区间 [i, m) 中
|
||||
j = m
|
||||
} else { // 找到目标元素,返回其索引
|
||||
} else { // 找到目标元素,返回其索引
|
||||
return m
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,8 +55,8 @@ comments: true
|
||||
```go title="hashing_search.go"
|
||||
/* 哈希查找(数组) */
|
||||
func hashingSearchArray(m map[int]int, target int) int {
|
||||
// 哈希表的 key: 目标元素,value: 索引
|
||||
// 若哈希表中无此 key ,返回 -1
|
||||
// 哈希表的 key: 目标元素,value: 索引
|
||||
// 若哈希表中无此 key ,返回 -1
|
||||
if index, ok := m[target]; ok {
|
||||
return index
|
||||
} else {
|
||||
|
||||
@@ -206,8 +206,8 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title="linear_search.go"
|
||||
/* 线性查找(链表)*/
|
||||
func linerSearchLinkedList(node *ListNode, target int) *ListNode {
|
||||
/* 线性查找(链表) */
|
||||
func linearSearchLinkedList(node *ListNode, target int) *ListNode {
|
||||
// 遍历链表
|
||||
for node != nil {
|
||||
// 找到目标结点,返回之
|
||||
|
||||
Reference in New Issue
Block a user