当前位置:网站首页>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
边栏推荐
- 你需要社交媒体二维码的21个理由
- 卡鱼刺别再喝醋吞米饭了!教你2招,让鱼刺安全“跑出来”
- 890. find and replace mode
- 元宇宙链游与传统游戏的区别
- MATLAB中stairs函数使用
- AcWing 1921. Rearranging cows (ring diagram)
- Zabbix 监控之LLD
- Stream as a return value in WCF - who disposes of it- Stream as a return value in WCF - who disposes it?
- DS18B20 digital thermometer (I) electrical characteristics, parasitic power supply mode and remote wiring
- LVS health state detection based on application layer
猜你喜欢

Grid layout
![[Blue Bridge Cup SCM 11th National race]](/img/da/3c8a9efd5b28f67816f239531a0339.png)
[Blue Bridge Cup SCM 11th National race]

信号继电器RXSF1-RK271018DC110V

AcWing 128. 编辑器(对顶栈 实现序列内部指定位置高效修改)

记录一下使用JPA时遇到的坑

Clickhouse column basic data type description

^33变量提升和函数提升面试题

Reading mysql45 lecture - self summary (part)

卡鱼刺别再喝醋吞米饭了!教你2招,让鱼刺安全“跑出来”

AcWing 131. The largest rectangle in the histogram (monotone stack classic application template)
随机推荐
AcWing 135. Maximum subsequence sum (prefix sum + monotone queue to find the minimum value of fixed length interval)
重量级代理缓存服务器Squid
AcWing 132. Group queue (queue simulation question)
Golang Foundation (6)
Don't swallow rice with vinegar! Teach you 2 moves to make the fish bones "run out" safely
DS18B20 digital thermometer (I) electrical characteristics, parasitic power supply mode and remote wiring
B+ 树的简单认识
SOT23(Small Outline Transistor)
AcWing 1986. Mirror (simulation, ring diagram)
Create vite source code analysis
AI - face
AI - face
LLD monitored by ZABBIX
postman传入list
网络的拓扑结构
A simple understanding of b+ tree
Les humains veulent de l'argent, du pouvoir, de la beauté, de l'immortalité, du bonheur... Mais les tortues ne veulent être qu'une tortue.
【CF1392D】D. Omkar and Bed Wars(环形与后效性dp)
arm各种交叉编译工具的区别
Lambda表达式 | 浅解