mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
18 lines
315 B
Go
18 lines
315 B
Go
package leetcode
|
|
|
|
func searchInsert(nums []int, target int) int {
|
|
low, high := 0, len(nums)-1
|
|
for low <= high {
|
|
mid := low + (high-low)>>1
|
|
if nums[mid] >= target {
|
|
high = mid - 1
|
|
} else {
|
|
if (mid == len(nums)-1) || (nums[mid+1] >= target) {
|
|
return mid + 1
|
|
}
|
|
low = mid + 1
|
|
}
|
|
}
|
|
return 0
|
|
}
|