当前位置:网站首页>35. Search insert position
35. Search insert position
2022-07-27 07:18:00 【DXB2021】
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
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 .
You must write an algorithm with O(log n) runtime complexity.
Please use a time complexity of O(log n) The algorithm of .
Example 1: Example 1:
Input: nums = [1,3,5,6], target = 5
Output: 2
Input : nums = [1,3,5,6], target = 5 Output : 2
Example 2: Example 2:
Input: nums = [1,3,5,6], target = 2
Output: 1
Input : nums = [1,3,5,6], target = 2 Output : 1
Example 3: Example 3:
Input: nums = [1,3,5,6], target = 7
Output: 4
Input : nums = [1,3,5,6], target = 7 Output : 4
Constraints: Tips :
1 <= nums.length <= 
-
<= nums[i] <= 
nums contains distinct values sorted in ascending order.
numsby No repeating elements Of Ascending Arrange arrays
-
<= target <= 
C Language :
int searchInsert(int* nums, int numsSize, int target){
int index=0;
for(int i=0;i<numsSize;i++)
{
if(nums[i]<target)
{
if(i+1==numsSize)
{
index=i+1;
break;
}
else if(nums[i+1]>=target)
{
index=i+1;
break;
}
}
else if(nums[i]==target)
{
index=i;
break;
}
}
return index;
}Execution results : adopt
Execution time :4 ms, In all C Defeated in submission 94.53% Users of
Memory consumption :5.7 MB, In all C Defeated in submission 97.52% Users of
Pass the test case :64 / 64
from 《 Two points search 》 On , Learned binary numbers , One bit to the right is divided by 2 Number of numbers , One bit to the left is multiplied by 2 Number of numbers .
C Language :
int searchInsert(int* nums, int numsSize, int target){
int left=0,right=numsSize-1,index=numsSize;
while(left<=right)
{
int mid=((right-left)>>1)+left;
if(target<=nums[mid]){
index=mid;
right=mid-1;
}else{
left=mid+1;
}
}
return index;
}Execution results : adopt
Execution time :4 ms, In all C Defeated in submission 94.53% Users of
Memory consumption :5.8 MB, In all C Defeated in submission 69.62% Users of
Pass the test case :64 / 64
边栏推荐
- Using docker to install and deploy redis on CentOS
- How does golang assign values to empty structures
- Leetcode series (I): buying and selling stocks
- Analysis of online and offline integration mode of o2o E-commerce
- Drools (5): drools basic syntax (3)
- Drools(5):Drools高级语法
- Calledprocesserror during pre commit install
- 想sink 到 redis-hash 里面 把 对象的属性和值都写进去 ,大佬们有Demo 吗?
- Watermelon book learning notes - Chapter 4 decision tree
- Interpretation of deepsort source code (IV)
猜你喜欢

美联储SR 11-7:模型风险管理指南(Guidance on Model Risk Management)-万字收藏

Pan Aimin, chairman of instruction set, attended the 2022 ecug con to speak for China's technical forces

Vscode connection remote server development

Working principle analysis of deepsort

把Excel转换成CSV/CSV UTF-8

Consideration on how the covariance of Kalman filter affects the tracking effect of deepsort
![Jest single test style problem [identity obj proxy] NPM package](/img/1d/7845a6da9c09bfdde7676a37579225.png)
Jest single test style problem [identity obj proxy] NPM package

sql-labs SQL注入平台-第1关Less-1 GET - Error based - Single quotes - String(基于错误的GET单引号字符型注入)

Book borrowing management system based on SSM

Automatically generate UML sequence diagram according to text (draw.io format)
随机推荐
内部类--看这篇就懂啦~
(posted) comparison of Eureka, consumer and Nacos 2
Interpretation of deepsort source code (I)
Cass11.0.0.4 for autocad2010-2023 dog free usage
Usage of string class
指令集 x 数澜科技丨加速政企数字化转型,打造DT领域独角兽企业联盟
ShowDoc漏洞学习——CNVD-2020-26585(任意文件上传)
Misunderstanding of slice slice in golang
Relevant principles of MySQL index optimization
使用pip命令切换不同的镜像源
R2live code learning record (3): radar feature extraction
How to implement Devops with automation tools | including low code and Devops application practice
The qualities that a technical manager should have (guess)
35. Search Insert Position 搜索插入位置
Drools (5): drools basic syntax (3)
把Excel转换成CSV/CSV UTF-8
pytorch笔记:TD3
Pytorch model
Generics -- learn it, and there are many benefits
Confluence漏洞学习——CVE-2021-26084/85,CVE-2022-26134漏洞复现