We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f60060b commit 846f281Copy full SHA for 846f281
go/search_insert_position.go
@@ -3,20 +3,16 @@ package main
3
4
func searchInsert(nums []int, target int) int {
5
left, right := 0, len(nums)
6
- var mid int
7
for left < right {
8
- mid = (left + right) / 2
9
- if target == nums[mid] {
+ mid := (left + right) / 2
+ if nums[mid] == target {
10
return mid
11
}
12
- if target < nums[mid] {
13
- right = mid
14
- } else {
+ if nums[mid] < target {
15
left = mid + 1
+ } else {
+ right = mid
16
17
18
19
- return mid
20
- }
21
- return mid + 1
+ return left
22
0 commit comments