当前位置:网站首页>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;
};
边栏推荐
- Mysql优化
- Simulation of future air pollution changes
- New knowledge in big homework
- Alibaba three sides: how to solve the problems of MQ message loss, duplication and backlog?
- LeetCode链表问题——206.反转链表(一题一文学会链表)
- C语言力扣第42题之接雨水。四种方法——暴力、动态规划、栈、双指针
- JVM Lecture 5: how to deal with peak push of vertical and horizontal data
- The pit of history can only be filled up as far as possible
- Seata submits at details in two stages
- When AQS wakes up the thread, I understand why it traverses from the back to the front
猜你喜欢

Seata submits at details in two stages

What are the characteristics of the grammar of Russian documents in the translation of scientific papers

Black eat black? The man cracked the loopholes in the gambling website and "collected wool" for more than 100000 yuan per month

Excel VBA: summarize calculation output results by date (SUMIF)

uniapp小程序框架-一套代码,多段覆盖

Textfield and password input box that are more flexible and easy to use in compose

Embedded sharing collection 20

Security permission management details

Redis过期删除策略和内存淘汰策略

开发转测试:从零开始的6年自动化之路
随机推荐
Interprocess communication
The pit of history can only be filled up as far as possible
Mysql主从同步及主从同步延迟解决方案
The landing of tdengine in the GPS and AIS scheduling of Zhongtian steel
CMD operation command
MySQL八股知识点:从入门到删库
科技论文翻译,俄语文档的语法有何特点
AXI协议(5):AXI协议的burst机制
YOLOv5执行全过程----目录
Distance between bus stops: simple simulation problem
CLM land surface process model
Date and time function of MySQL function summary
Nacos registry
阿里云工业视觉智能工程师ACP认证——备考
@Principle of Autowired annotation
An online accident, I suddenly realized the essence of asynchrony
List转换为tree-项目真实使用
security权限管理详解
Simulation of future air pollution changes
Excel VBA:实现自动下拉填充公式至最后一行