当前位置:网站首页>leetcode每日一题——搜索插入位置
leetcode每日一题——搜索插入位置
2022-06-11 14:08:00 【java小豪】
35. 搜索插入位置
【题目描述】
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
请必须使用时间复杂度为 O(log n) 的算法。
【示例输入】
示例 1:
输入: nums = [1,3,5,6], target = 5
输出: 2
示例 2:
输入: nums = [1,3,5,6], target = 2
输出: 1
示例 3:
输入: nums = [1,3,5,6], target = 7
输出: 4
【思路】
● 先设定左侧下标 left 和右侧下标 right,再计算中间下标 mid
● 每次根据 nums[mid] 和 target 之间的大小进行判断,相等则直接返回下标,nums[mid]<target 则 left 右移,nums[mid] > target 则 right 左移
● 查找结束如果没有相等值则返回 left,该值为插入位置
【解法一】:二分法
class Solution {
public int searchInsert(int[] nums, int target) {
int len = nums.length;
int left = 0, rigth = len - 1, ans = len;
while(left <= rigth) {
int mid = left + ((rigth - left) / 2);
if (target <= nums[mid]) {
ans = mid;
rigth = mid - 1;
} else {
left = mid + 1;
}
}
return ans;
}
}
边栏推荐
- 线程池的七个参数与拒绝策略
- Pond (topology + priority queue)
- Question bank and answers for 2022 tool fitter (intermediate) operation certificate examination
- Container -- reverse content -- use of explosion, splicing, and inversion functions
- 2022工具钳工(中级)操作证考试题库及答案
- Please, don't use enumeration types in external interfaces any more!
- Each of D is inconsistent with the map
- Current situation and future development trend of scaffold market in the world and China from 2022 to 2028
- couldn‘t upgrade db schema: insert into ACT_GE_PROPERTY values (‘common.sche[已解决]
- 二十八-三维点云实时和离线生成二维栅格、三维栅格地图
猜你喜欢

强大的全文本搜索工具——AnyTXT Searcher

Ali talked about the use of strategic mode in the project

Task manager based on Qt development

mysql创建表出错1067 - Invalid default value for ‘update_time‘

Leetcode 1968. 构造元素不等于两相邻元素平均值的数组(可以,终于解决)

代码对比工具,我就用这6个

折叠表达式

MySQL create table error 1067 - invalid default value for 'update_ time‘

Why does each running Department display that the database already exists, delete the database, and then succeed? Each running department must delete the database, and re run the whole successfully

How to quickly make the title and ending with one click?
随机推荐
D interval to nullable conversion
Private collection project practice sharing [Yugong series] February 2022 wechat applet -app Other properties of JSON configuration properties
基于Qt开发实现的任务管理器
Current situation and future development trend of precision air conditioning market in the world and China
Task manager based on Qt development
高通WLAN框架学习(29)-- 6GHz 概述
C. Mortal Kombat Tower(cf)dp
The application of machine learning in database cardinality estimation
2022 Gansu Province safety officer B certificate test question simulation test question bank and online simulation test
[public class preview]: mxplayer Ott audio and video transcoding practice and optimization
In depth research and analysis report on global and Chinese spray drying machinery market
In depth research and analysis report on global and Chinese content audit market
Leetcode 1968. Construct an array whose elements are not equal to the average value of two adjacent elements (yes, finally solved)
Vi LXD deployment of lab server for development records
阿里一面,谈谈策略模式在项目中的使用
gensim.models word2vec 参数
2022年全国最新消防设施操作员(初级消防设施操作员)题库及答案
Recommended open source scheduling libraries worth learning
Question bank and answers of the latest national fire-fighting facility operators (primary fire-fighting facility operators) in 2022
Work summary: it took a long time to write SQL because of Cartesian product problem (Cartesian product summary attached)