当前位置:网站首页>35. Search the insertion position
35. Search the insertion position
2022-07-26 05:10:00 【rananie】
List of articles
35. Search insert location
subject
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
source : Power button (LeetCode)
link :https://leetcode.cn/problems/search-insert-position
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Answer key
Where to find the target value in an ordered array , Use dichotomy
function searchInsert(nums: number[], target: number): number {
let left = 0;
let right = nums.length-1;
let mid:number;
while(left<=right){
mid = (left+right) >> 1;
if(nums[mid]==target)return mid;
else if(nums[mid]<target) left = mid+1; // It shows that the result is in the right half interval
else right = mid-1;// It shows that the result is in the left half interval
}
// It means that we didn't find
return left;
};
This problem can also be solved violently
function searchInsert(nums: number[], target: number): number {
for(let i=0;i<nums.length;++i){
// If it is smaller than the target, continue to look down , The position of the first value larger or equal to the target is the position of the inserted value
if(nums[i]>=target)return i ;
}
return nums.length;
};
边栏推荐
- Meta analysis [whole process, uncertainty analysis] method based on R language and meta machine learning
- CountLaunch Demo的测试
- 异步时父子线程间的ThreadLocal传递问题
- Ansible tutorial
- Improve reduce parallelism in shuffle operation
- Google Emoji guessing game helps parents guide their children to surf the Internet safely
- 【洛谷】P1383 高级打字机
- 基于R语言的Meta分析【全流程、不确定性分析】方法与Meta机器学习
- AXI协议(4):AXI通道上的信号
- unity场景跳转脚本
猜你喜欢

嵌入式分享合集21

MySQL八股知识点:从入门到删库

Switch and router technology: dynamic routing protocol, rip routing protocol and OSPF routing protocol

Install nccl \ mpirun \ horovod \ NVIDIA tensorflow (3090ti)

CLM land surface process model

注解@Autowired如何自动装配

C语言-指针进阶

Simulation of future air pollution changes

【ACWing】2983. 玩具

DOM事件流 事件冒泡-事件捕获-事件委托
随机推荐
Migrate the server and reconfigure the database (the database has no monitoring, and the monitoring starts with tns-12545, tns-12560, tns-00515 errors)
Axi protocol (4): signals on the Axi channel
Excel vba: saving multiple worksheets as new files
遥感、GIS和GPS技术在水文、气象、灾害、生态、环境及卫生等领域中的应用
The importance of supporting horizontal expansion of time series database
Common solutions for distributed ID - take one
Shell的read 读取控制台输入、read的使用
基于遥感解译与GIS技术环境影响评价图件制作
你对“happen-before原则”的理解可能是错的?
ABAP语法学习(ALV)
When AQS wakes up the thread, I understand why it traverses from the back to the front
提高shuffle操作中的reduce并行度
Alibaba three sides: how to solve the problems of MQ message loss, duplication and backlog?
Uniapp applet framework - a set of code, multi segment coverage
Nacos 介绍和部署
Excel VBA:将多个工作表保存为新文件
MySQL基础学习
推荐必读:测试人员如何快速熟悉新业务?
MySQL basic learning
域名解析过程全分析,就着文字理解更佳