Files
leetcode/top-100-liked/63/leetcode_test.go
T
2026-07-15 20:46:11 +08:00

39 lines
737 B
Go

package top100liked
import (
"fmt"
"testing"
)
// https://leetcode.cn/problems/search-insert-position/description/?envType=study-plan-v2&envId=top-100-liked
func searchInsert(nums []int, target int) int {
start, end := 0, len(nums)-1
for start < end {
mid := start + (end-start)/2
if nums[mid] == target {
return mid
} else if nums[mid] > target {
end = mid - 1
} else {
start = mid + 1
}
}
if nums[end] < target {
return end + 1
}
return end
}
func Test(t *testing.T) {
fmt.Printf("%+v\n", searchInsert([]int{1, 3, 5, 6}, 5))
}
func Test2(t *testing.T) {
fmt.Printf("%+v\n", searchInsert([]int{1, 3, 5, 6}, 2))
}
func Test3(t *testing.T) {
fmt.Printf("%+v\n", searchInsert([]int{1, 3, 5, 6}, 7))
}