当前位置:网站首页>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 (VII)
- (转帖)eureka、consul、nacos的对比2
- 2021 interview questions for php+go of Zhongda factory (2)
- Drools(5):Drools高级语法
- 2021 interview question of php+go for Zhongda factory (1)
- pytorch笔记:TD3
- 如何借助自动化工具落地DevOps|含低代码与DevOps应用实践
- Leetcode series (I): buying and selling stocks
- LogCat工具
- [Vani has a date] tail on rainy days
猜你喜欢

Gbase 8C product introduction

Digital image processing Chapter 1 Introduction

Pytorch notes: td3

Vscode connection remote server development

如何借助自动化工具落地DevOps|含低代码与DevOps应用实践

Firefox browser, when accessing Tencent cloud server, failed to establish a secure connection.

pytorch笔记:TD3

A Competitive Swarm Optimizer for Large Scale Optimization

Esp8266 (esp-12f) third party library use -- sparkfun_ Apds9960 (gesture recognition)

Bert and RESNET can also be trained on mobile phones?!
随机推荐
Livox SLAM(带LIO+闭环检测优化)
DDD Domain Driven Design Notes
算法--斐波那契数列(Kotlin)
Drools (5): drools basic syntax (3)
Interpretation of deepsort source code (III)
Talk about multimodality of fire
Algorithm -- Fibonacci sequence (kotlin)
Overall dichotomy?
PHP defines the array using commas,
【golang学习笔记2.1】 golang中的数组中的排序和查找
Web configuration software for industrial control is more efficient than configuration software
Analysis on the current situation and optimization strategy of customer experience management in banking industry
二叉树--天然的查找语义(1)基础篇
Vscode creates golang development environment and debug unit test of golang
MySQL query operation index optimization practice
Gbase 8C product introduction
Brief introduction of simulation model
指令集 x 数澜科技丨加速政企数字化转型,打造DT领域独角兽企业联盟
Codeforces Round #809 (Div. 2)(6/6)(Kruskal重构树)
The vscode run command reported an error: the mark "&" is not a valid statement separator in this version.