mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-12-27 04:46:17 +00:00
add a code in go
This commit is contained in:
parent
44004ea38f
commit
5fcc995b70
@ -40,6 +40,8 @@
|
||||
|
||||
#### 题目代码
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public int searchInsert(int[] nums, int target) {
|
||||
@ -66,5 +68,28 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func searchInsert(nums []int, target int) int {
|
||||
left, right := 0, len(nums) - 1
|
||||
for (left <= right) {
|
||||
mid := left + ((right - left) >> 1)
|
||||
// 查询成功
|
||||
if nums[mid] == target {
|
||||
return mid
|
||||
// 右区间
|
||||
} else if nums[mid] < target {
|
||||
left = mid + 1
|
||||
// 左区间
|
||||
} else {
|
||||
right = mid - 1
|
||||
}
|
||||
}
|
||||
return left
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user