Skip to content

Latest commit

 

History

History
38 lines (34 loc) · 591 Bytes

35.搜索插入位置.md

File metadata and controls

38 lines (34 loc) · 591 Bytes
title categories
搜索插入位置
leetcode
leetcode题解
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