Skip to content

Commit 846f281

Browse files
committed
refactor Search Insert Position
1 parent f60060b commit 846f281

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

go/search_insert_position.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,16 @@ package main
33

44
func searchInsert(nums []int, target int) int {
55
left, right := 0, len(nums)
6-
var mid int
76
for left < right {
8-
mid = (left + right) / 2
9-
if target == nums[mid] {
7+
mid := (left + right) / 2
8+
if nums[mid] == target {
109
return mid
1110
}
12-
if target < nums[mid] {
13-
right = mid
14-
} else {
11+
if nums[mid] < target {
1512
left = mid + 1
13+
} else {
14+
right = mid
1615
}
1716
}
18-
if target < nums[mid] {
19-
return mid
20-
}
21-
return mid + 1
17+
return left
2218
}

0 commit comments

Comments
 (0)