当前位置:网站首页>LeetCode_ 35 (search insertion position)
LeetCode_ 35 (search insertion position)
2022-07-01 04:44:00 【***】
Title Description :
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
class Solution {
public int searchInsert(int[] nums, int target) {
int res=-1;
int left=0,right=nums.length-1,pos=0;
while(left<=right){
pos=(left+right)/2;
if(target>nums[pos])left=pos+1;
else if(target<nums[pos])right=pos-1;
else if(target==nums[pos])return pos;
if(left>right)return left;
}
return res;
}
}
边栏推荐
- 数据加载及预处理
- 无器械健身
- Pytorch(三) —— 函数优化
- Shell之Unix运维常用命令
- RDF query language SPARQL
- 2022 gas examination question bank and online simulation examination
- Registration for R2 mobile pressure vessel filling test in 2022 and R2 mobile pressure vessel filling free test questions
- pytorch 卷积操作
- 常用的Transforms中的方法
- LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target
猜你喜欢
Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022
Kodori tree board
STM32扩展板 数码管显示
分布式数据库数据一致性的原理、与技术实现方案
Question bank and answers for chemical automation control instrument operation certificate examination in 2022
Applications and features of VR online exhibition
VIM easy to use tutorial
Pytorch(二) —— 激活函数、损失函数及其梯度
Dataloader的使用
STM32扩展板 温度传感器和温湿度传感器的使用
随机推荐
C -- array
Question bank and answers for chemical automation control instrument operation certificate examination in 2022
[pat (basic level) practice] - [simple simulation] 1064 friends
Shell analysis server log command collection
Shell之一键自动部署Redis任意版本
Daily question - line 10
Offline installation of Wireshark 2.6.10
【硬十宝典】——2.【基础知识】开关电源各种拓扑结构的特点
TCP server communication flow
Openresty rewrites the location of 302
2022年G1工业锅炉司炉特种作业证考试题库及在线模拟考试
js解决浮点数相乘精度丢失问题
神经网络-使用Sequential搭建神经网络
CF1638E. Colorful operations Kodori tree + differential tree array
Leecode question brushing record 1310 subarray XOR query
Summary of testing experience - Testing Theory
Dual contractual learning: text classification via label aware data augmentation reading notes
slf4j 简单实现
Common UNIX Operation and maintenance commands of shell
Execution failed for task ‘:app:processDebugResources‘. > A failure occurred while executing com. and