当前位置:网站首页>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;}} 边栏推荐
- 图形处理单元(GPU)的演进
- 免费的中英文翻译软件-自动批量中英文翻译软件推荐大全
- LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一
- Jest 测试框架 beforeEach 的设计原理解析
- Nanny Level Tutorial: Write Your Own Mobile Apps and Mini Programs (Part 2)
- OSI 七层模型和TCP/IP模型及对应协议(详解)
- AdguardHome如何配置设置?我的AdguardHome配置内容过滤器拦截列表
- List排序 ,取最大值最小值
- ES2020-23简单易懂又实用的精选特性讲解 日常开发必备干货!
- JSP中如何正确的填写include指令中的file路径呢?
猜你喜欢

QAbstractScrollArea、QScrollArea

OSI 七层模型和TCP/IP模型及对应协议(详解)

ssm网页访问数据库数据报错

Nanny Level Tutorial: Write Your Own Mobile Apps and Mini Programs (Part 2)

Camera Hal OEM模块 ---- cmr_snapshot.c

jacoco的学习以及理解

CAN总线的AUTOSAR网络管理

AQS-AbstractQueuedSynchronizer

19、商品微服务-srv层实现

Crack detection technology based on deep learning
随机推荐
微信小程序---组件开发与使用
运行yum报错Error: Cannot retrieve metalink for reposit
Camera Hal OEM模块 ---- cmr_snapshot.c
npm install报错npm ERR Could not resolve dependency npm ERR peer
ansible module --copy module
CCF paper conference IEEE how to query all articles of a conference journal
记录代码
How to technically ensure the quality of LED display?
Question about #oracle#, how to solve it?
npm run dev 和 npm run serve区别
【kali-信息收集】(1.8)ARP侦查工具_Netdiscover
Excel dynamic chart production
Create a devops CI/CD process using the kubesphere GUI
【项目管理技术的优势】
Kotlin的协程与生命周期
leetcode: 200. Number of islands
流动性质押挖矿系统开发如何制作?单双币系统开发成熟技术
SQLAlchemy使用教程
打破千篇一律,DIY属于自己独一无二的商城
CCF论文会议 IEEE 如何查询某个会议期刊的所有文章