当前位置:网站首页>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;
}
}
边栏推荐
- Leetcode 1962. Remove stones to minimize the total amount (should be rounded up)
- Ponds(拓扑 + 优先队列)
- 2022工具钳工(中级)操作证考试题库及答案
- [public class preview]: mxplayer Ott audio and video transcoding practice and optimization
- 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
- Redis uses 10 tips, please accept!
- In depth research and analysis report on global and Chinese one component liquid rubber Market
- Container -- reverse content -- use of explosion, splicing, and inversion functions
- Introduction to reverse learning - excellent assembly debugging tool OllyDbg
- JSTL 自定义标签
猜你喜欢

Just after the college entrance examination, I was confused and didn't know what to do? Tell me what I think

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

Introduction to reverse learning - excellent assembly debugging tool OllyDbg

XXVIII - 3D point cloud real-time and offline generation of 2D grid and 3D grid map

Invalid bound statement (not found)错误【已解决】

Collapse expression

Three level classification display

阿里一面,谈谈策略模式在项目中的使用

可变参表达式
随机推荐
强大的全文本搜索工具——AnyTXT Searcher
couldn‘t upgrade db schema: insert into ACT_GE_PROPERTY values (‘common.sche[已解决]
2022工具钳工(中级)操作证考试题库及答案
Work summary: it took a long time to write SQL because of Cartesian product problem (Cartesian product summary attached)
I have something to say about final, finally and finalize
In depth research and analysis report on global and Chinese diet food market
2022工具钳工(中级)操作证考试题库及答案
Code comparison tool, I use these six
C. Mortal Kombat Tower(cf)dp
How to quickly compress the size of video?
Part 23, two-way circular linked list model.
Variable parameter expression
2022工具钳工(中级)操作证考试题库及答案
Question bank and answers for 2022 tool fitter (intermediate) operation certificate examination
D interval to nullable conversion
In depth research and analysis report on global and Chinese sanitary safety product market
使用cpolar远程办公(1)
Please, don't use enumeration types in external interfaces any more!
Top 10 bone conduction earphones in the list, and five easy-to-use bone conduction earphones are recommended
Sqlmap detection SQL lab range