add a code in go

This commit is contained in:
lincongcong 2021-07-23 23:50:29 +08:00
parent 44004ea38f
commit 5fcc995b70

View File

@ -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
}
```