当前位置:网站首页>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
边栏推荐
- Interpretation of deepsort source code (III)
- 【golang学习笔记2.1】 golang中的数组中的排序和查找
- 2021 interview questions for php+go of Zhongda factory (2)
- Day012 application of one-dimensional array
- Oracle数据库问题
- Confluence漏洞学习——CVE-2021-26084/85,CVE-2022-26134漏洞复现
- MySQL quickly compares database table data
- 李沐动手学深度学习V2-transformer和代码实现
- Knowledge points and answers of PHP intermediate interview in 2020
- ?实验 7 基于 Mysql 的 PHP 管理系统实现
猜你喜欢

A Competitive Swarm Optimizer for Large Scale Optimization

Talk about multimodality of fire

Gbase 8C product introduction

Digital image processing Chapter 1 Introduction
![Jest single test style problem [identity obj proxy] NPM package](/img/1d/7845a6da9c09bfdde7676a37579225.png)
Jest single test style problem [identity obj proxy] NPM package

零号培训平台课程-1、SQL注入基础

Bert and RESNET can also be trained on mobile phones?!

How to learn C language? This article gives you the complete answer

Overall dichotomy?

Watermelon book learning notes - Chapter 4 decision tree
随机推荐
高级IO提纲
sql-labs SQL注入平台-第1关Less-1 GET - Error based - Single quotes - String(基于错误的GET单引号字符型注入)
Linear table -- stack and queue
Netease Yunxin appeared at the giac global Internet architecture conference to decrypt the practice of the new generation of audio and video architecture in the meta universe scene
Vscode connection remote server development
35. Search Insert Position 搜索插入位置
Golang encapsulates the packages involved in MySQL and the differences between sqlx and Gorm
Calledprocesserror during pre commit install
String类的用法
Generics -- learn it, and there are many benefits
Ci framework learning of PHP
(posted) comparison of Eureka, consumer and Nacos 2
12. Integer to Roman整数转罗马数字
美联储SR 11-7:模型风险管理指南(Guidance on Model Risk Management)-万字收藏
请问 mysql timestamp(6) 用flink-sql接过来是 null,这点有办法处理不
A Competitive Swarm Optimizer for Large Scale Optimization
TS learning (VIII): classes in TS
Digital image processing -- Chapter 3 gray scale transformation and spatial filtering
Codeworks round 809 (Div. 2) (6/6) (Kruskal reconstruction tree)
Analysis of strong tennis cup 2021 PWN competition -- babypwn