当前位置:网站首页>35. search insertion position
35. search insertion position
2022-06-12 11:25:00 【Blue cotton】
35. Search insert location
Given a sort array and a target value , Find the target value in the array , And return its index . If the target value does not exist in the array , Return to where it will be inserted in sequence .
Please use a time complexity of O(log n) The algorithm of .
Example 1:
Input : nums = [1,3,5,6], target = 5
Output : 2
Example 2:
Input : nums = [1,3,5,6], target = 2
Output : 1
Example 3:
Input : nums = [1,3,5,6], target = 7
Output : 4
Tips :
1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums by No repeating elements Of Ascending Arrange arrays
-104 <= target <= 104
Their thinking :
- Solve with dichotomy
- First set the left subscript left And right subscript right, Then calculate the middle subscript mid
- Per basis nums[mid] and target Judge the size between , Equal returns the subscript directly ,
- nums[mid] < target be left Move right ,
- nums[mid] > target be right Move left
- End of search. If there is no equal value, return left, This value is the insertion position
class Solution {
public int searchInsert(int[] nums, int target) {
int right = nums.length-1;
int left = 0;
int mid = 0;
while(left <= right){
mid = (left+right) / 2;
if(nums[mid] == target){
return mid;
}else if(nums[mid] < target){
left = mid + 1;
}else if(nums[right] > target){
right = mid - 1;
}
}
return left;
}
}
Reference resources :LeetCode
边栏推荐
- mysql中的索引show index from XXX每个参数的意义
- FPGA-按键实验
- 【CF1392D】D. Omkar and Bed Wars(环形与后效性dp)
- 力扣(LeetCode)162. 寻找峰值(2022.06.11)
- DS18B20 digital thermometer (I) electrical characteristics, parasitic power supply mode and remote wiring
- FormatConversionTool.exe
- 网络的拓扑结构
- 【clickhouse专栏】基础数据类型说明
- mysql的悲观锁和乐观锁
- LLD monitored by ZABBIX
猜你喜欢

SOT23(Small Outline Transistor)

十折交叉验证代码中的问题

MCUXpresso开发NXP RT1060(3)——移植LVGL到NXP RT1060

Why check the @nonnull annotation at run time- Why @Nonnull annotation checked at runtime?

AcWing 132. 小组队列(队列模拟题)

Socket programming UDP

Signal relay rxsf1-rk271018dc110v

890. 查找和替换模式

Module 8 job

k58.第一章 基于kubeadm安装kubernetes v1.23 -- 集群部署
随机推荐
Epidemic home office experience | community essay solicitation
k58.第一章 基于kubeadm安装kubernetes v1.23 -- 集群部署
Collation of common functions in JS
元宇宙链游与传统游戏的区别
MySQL锁查漏补缺
B+ 树的简单认识
Reading mysql45 lecture - self summary (part)
Clj3-100alh30 residual current relay
深度学习与CV教程(14) | 图像分割 (FCN,SegNet,U-Net,PSPNet,DeepLab,RefineNet)
Handwritten common interview questions
Php中redis的keys问题
【蓝桥杯单片机 国赛 第十一届】
网络的拓扑结构
Unity connect to Microsoft SQLSERVER database
Distributed storage exploration
K58. Chapter 1 installing kubernetes V1.23 based on kubeadm -- cluster deployment
ReentrantLock源码分析
The reason why scanf return value is ignored and its solution
SOT23(Small Outline Transistor)
InfoQ geek media's 15th anniversary solicitation |position:fixed virtual button cannot take effect after being triggered. Problem analysis and Solution Exploration