当前位置:网站首页>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;
}
}
边栏推荐
- Codeforces Round #771 (Div. 2) ABCD|E
- Shell之分析服务器日志命令集锦
- C -- array
- Why is Internet thinking not suitable for AI products?
- STM32 photoresistor sensor & two channel AD acquisition
- JS rotation chart
- 2022 t elevator repair question bank and simulation test
- Odeint et GPU
- Registration of P cylinder filling examination in 2022 and analysis of P cylinder filling
- Pytorch(三) —— 函数优化
猜你喜欢
随机推荐
PgSQL failed to start after installation
洗个冷水澡吧
2022 gas examination question bank and online simulation examination
Leecode question brushing record 1332 delete palindrome subsequence
Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor
2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank
分布式全局唯一ID解决方案详解
LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target
Odeint et GPU
常用的Transforms中的方法
分布式架构系统拆分原则、需求、微服务拆分步骤
神经网络-卷积层
RuntimeError: mean(): input dtype should be either floating point or complex dtypes.Got Long instead
STM32 光敏电阻传感器&两路AD采集
This sideline workload is small, 10-15k, free unlimited massage
Registration of P cylinder filling examination in 2022 and analysis of P cylinder filling
Seven crimes of counting software R & D Efficiency
Pytorch(一) —— 基本语法
无器械健身
[difficult] sqlserver2008r2, can you recover only some files when recovering the database?









