当前位置:网站首页>068.查找插入位置--二分查找
068.查找插入位置--二分查找
2022-07-06 09:01:00 【棱镜7】
一、code
1.1 题目描述
1.2 题解
class Solution {
// 二分查找
public int searchInsert(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
if(nums[left] > target) return 0; //左边界
if(nums[right] < target) return nums.length; //有边界
while(left <= right){
int mid = left + (right - left) / 2;
if(nums[mid] == target) return mid;
if(nums[mid] > target){
if(nums[mid - 1] < target){
return mid;
}
right = mid - 1;
}else{
left = mid + 1;
}
}
return 0;
}
}
二、总结
边栏推荐
- Basic usage of xargs command
- Appears when importing MySQL
- Global and Chinese markets of SERS substrates 2022-2028: Research Report on technology, participants, trends, market size and share
- LeetCode:26. Remove duplicates from an ordered array
- Redis之哨兵模式
- CUDA implementation of self defined convolution attention operator
- Leetcode problem solving 2.1.1
- [shell script] - archive file script
- 五层网络体系结构
- 【图的三大存储方式】只会用邻接矩阵就out了
猜你喜欢
IJCAI2022论文合集(持续更新中)
LeetCode41——First Missing Positive——hashing in place & swap
Solve the problem of inconsistency between database field name and entity class attribute name (resultmap result set mapping)
Mise en œuvre de la quantification post - formation du bminf
Activiti7工作流的使用
Redis之发布订阅
[three storage methods of graph] just use adjacency matrix to go out
Publish and subscribe to redis
Improved deep embedded clustering with local structure preservation (Idec)
[shell script] - archive file script
随机推荐
CSP salary calculation
Full stack development of quartz distributed timed task scheduling cluster
Intel distiller Toolkit - Quantitative implementation 1
Global and Chinese markets for hardware based encryption 2022-2028: Research Report on technology, participants, trends, market size and share
Redis' bitmap
Intel distiller Toolkit - Quantitative implementation 3
Nacos installation and service registration
QDialog
Show slave status \ read in G_ Master_ Log_ POS and relay_ Log_ The (size) relationship of POS
Global and Chinese market of appointment reminder software 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market for annunciator panels 2022-2028: Research Report on technology, participants, trends, market size and share
KDD 2022论文合集(持续更新中)
Simclr: comparative learning in NLP
Redis之持久化实操(Linux版)
Redis' performance indicators and monitoring methods
Global and Chinese market of metallized flexible packaging 2022-2028: Research Report on technology, participants, trends, market size and share
一改测试步骤代码就全写 为什么不试试用 Yaml实现数据驱动?
Redis之Bitmap
xargs命令的基本用法
[three storage methods of graph] just use adjacency matrix to go out