当前位置:网站首页>Likou 35 - search for insertion position - binary search
Likou 35 - search for insertion position - binary search
2022-08-02 11:45:00 【Zhang Dui Dui)】
Title description
Given a sorted 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, returns the position where it will be inserted in order.
Please use an O(log n) algorithm.
Solution ideas
- This is also a question of finding elements, the title requirement is to find elements in an ordered array and return the index;
- If the element is not in the array, it should return its index position;

- Then the target value may have four cases: ① It is smaller than the first element and placed at the front of the array; ② It is larger than the last element and inserted at the end of the array; ③ It is equal to a value in the array, and returnsIndex; ④ The value is not found in the array, find a suitable position to insert it
- It is conceivable that this kind of problem should be solved by the dichotomy method;
- At the beginning, judge the size relationship between target and the first and last elements of the array, and return the correct index;
- Use the left and right closed interval [ left , right ] in the search process;
- Just after judging all the cases, it should not return -1, it should return right+1, because right+1 is the index of the position to be inserted at this time.
Input and output example

Code
class Solution {public int searchInsert(int[] nums, int target) {int n = nums.length;int left = 0, right = n-1;//boolean flag = false;if(target < nums[0]){return 0;}else if(target > nums[n-1]){return n;}while(left <= right){int mid = left + ((right - left) >> 1);if(target == nums[mid]){//flag = true;return mid;}else if(target nums[mid]){//flag = true;left = mid + 1;}}return right+1;}} 边栏推荐
- AQS-AbstractQueuedSynchronizer
- SQL 数据更新
- npm WARN config global `--global`, `--local` are deprecated. Use `--location解决方案
- 使用kubesphere图形界面创建一个应用操作流程
- QT笔记——在一个窗口上显示另外一个透明窗口
- Problem solving in the process of using mosquitto
- SQL函数 TRIM
- AdguardHome如何配置设置?我的AdguardHome配置内容过滤器拦截列表
- Challenge LeetCode1000 questions in 365 days - Day 047 Design Circular Queue Circular Queue
- 观察者(observer)模式(二) —— 实现线程安全的监听器
猜你喜欢

JVM简介

npm run serve启动报错npm ERR Missing script “serve“

解决anaconda下载pytorch速度极慢的方法

5G网络切片技术

OLED的HAL库代码介绍及使用(stm32f1/I2C/HAL库版/100%一次点亮)

npm WARN config global `--global`, `--local` are deprecated. Use `--location解决方案

【Acunetix-忘记密码】
[email protected] This version of tar is no longer supported, and will not receive"/>npm WARN deprecated [email protected] This version of tar is no longer supported, and will not receive

使用kubesphere图形界面创建一个devops的CI/CD流程

sva assertion data
随机推荐
Hub and Spoke配置案例
微信小程序---组件开发与使用
【MySQL系列】- LIKE查询 以%开头一定会让索引失效吗
小程序插件的生态丰富,加速开发建设效率
基于深度学习的裂缝检测技术
sva 断言资料
注意力机制
10份重磅报告 — 展望中国数字经济未来
腾讯云云函数SCF—入门须知
【项目管理技术的优势】
匹配滤波(四种滤波器的幅频特性)
前男友买辣椒水威胁要抢女儿,女方能否申请人身安全保护令?
MySQL主从复制几个重要的启动选项
ASP.NET Core 6框架揭秘实例演示[31]:路由&ldquo;高阶&rdquo;用法
面积曲线AUC(area under curve)
SQL function TRIM
OLED的HAL库代码介绍及使用(stm32f1/I2C/HAL库版/100%一次点亮)
项目监控六大事项
[kali-information collection] (1.9) Metasploit + search engine tool Shodan
JSP中include指令的功能简介说明