Skip to content

Commit

Permalink
Merge pull request youngyangyang04#192 from betNevS/master
Browse files Browse the repository at this point in the history
添加 704. 二分查找 go 版本
  • Loading branch information
youngyangyang04 committed May 19, 2021
2 parents c98c8fb + 8e0ff0d commit e6db092
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion problems/0704.二分查找.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,52 @@ class Solution:

Go:

(版本一)左闭右闭区间

```go
func search(nums []int, target int) int {
high := len(nums)-1
low := 0
for low <= high {
mid := low + (high-low)/2
if nums[mid] == target {
return mid
} else if nums[mid] > target {
high = mid-1
} else {
low = mid+1
}
}
return -1
}
```

(版本二)左闭右开区间

```go
func search(nums []int, target int) int {
high := len(nums)
low := 0
for low < high {
mid := low + (high-low)/2
if nums[mid] == target {
return mid
} else if nums[mid] > target {
high = mid
} else {
low = mid+1
}
}
return -1
}
```





-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

0 comments on commit e6db092

Please sign in to comment.