当前位置:网站首页>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;}}
边栏推荐
猜你喜欢
【Acunetix-Forgot your password】
【Acunetix-忘记密码】
Camera Hal OEM模块 ---- cmr_snapshot.c
Multithreading (Basic) - 40,000 word summary
Mysql transaction isolation level and MVCC (multi-version concurrency control)
npm run dev 和 npm run serve区别
免费文档翻译-免费批量文档翻译软件推荐
excel 批量翻译-excel 批量函数公司翻译大全免费
[kali-information collection] (1.8) ARP reconnaissance tool _Netdiscover
AQS-AbstractQueuedSynchronizer
随机推荐
Create an application operation process using the kubesphere GUI
项目监控六大事项
基于深度学习的裂缝检测技术
Multithreading (Basic) - 40,000 word summary
阿苹的思考
Deep Learning 100 Examples - Convolutional Neural Network (CNN) for mnist handwritten digit recognition
智能手表前景如何?
小程序插件让开发者受益的几个理由
Several reasons why applet plugins benefit developers
流动性质押挖矿系统开发如何制作?单双币系统开发成熟技术
故障分析 | 一条 SELECT 语句跑崩了 MySQL ,怎么回事?
npm run serve启动报错npm ERR Missing script “serve“
Crack detection technology based on deep learning
ansible模块--copy模块
Crack detection technology based on deep learning
Outsourced Student Management System Architecture Documentation
【Acunetix-忘记密码】
Nanny Level Tutorial: Write Your Own Mobile Apps and Mini Programs (Part 2)
Coroutines and Lifecycle in Kotlin
今日睡眠质量记录85分