title | categories | ||
---|---|---|---|
搜索插入位置 |
|
package main
/*
* @lc app=leetcode.cn id=35 lang=golang
*
* [35] 搜索插入位置
*/
// [1,3,5,6]
// @lc code=start
func searchInsert(nums []int, target int) int {
low := 0
high := len(nums) - 1
for low <= high {
// midIndex := low + ((high - low) / 2)
midIndex := (high - low) / 2
midValue := nums[midIndex]
if target == midValue {
return midIndex
} else if target < midValue {
high = midIndex - 1
} else {
low = midIndex + 1
}
}
return high + 1
}
// @lc code=end