type
Post
status
Published
date
Jun 2, 2024
slug
summary
tags
算法
二分查找
数组
category
算法
icon
password
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
提示:
1 <= nums.length <= 104
104 <= nums[i] <= 104
nums为 无重复元素 的 升序 排列数组
104 <= target <= 104
示例 1:
示例 2:
示例 3:
思路:数组为有序数组,同时数组中无重复元素,一旦有重复元素,使用二分查找法返回的元素下标可能不是唯一的,这些都是使用二分法的前提条件,当看到题目描述满足如上条件的时候,要想一想是不是可以用二分法了。
边界条件判定:当区间为[x,x]闭区间时,左可以等于右;当为开区间时,左≠右。
- Author:guderain
- URL:https://wangguanxi.space/article/1562b727-a3a3-8084-a5ab-d75f09250234
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!
Relate Posts

.webp?table=collection&id=92be88af-5f71-4631-9d3e-ee3bd53dcced&t=92be88af-5f71-4631-9d3e-ee3bd53dcced&width=1080&cache=v2)
