当前位置:网站首页>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;
}
}
二、总结
边栏推荐
- QDialog
- 一篇文章带你了解-selenium工作原理详解
- Sentinel mode of redis
- 在QWidget上实现窗口阻塞
- Global and Chinese market of metallized flexible packaging 2022-2028: Research Report on technology, participants, trends, market size and share
- IJCAI2022论文合集(持续更新中)
- Minio distributed file storage cluster for full stack development
- Global and Chinese market of AVR series microcontrollers 2022-2028: Research Report on technology, participants, trends, market size and share
- Redis之Geospatial
- Activiti7工作流的使用
猜你喜欢
Nacos installation and service registration
基于B/S的医院管理住院系统的研究与实现(附:源码 论文 sql文件)
Redis cluster
[oc]- < getting started with UI> -- learning common controls
An article takes you to understand the working principle of selenium in detail
基于B/S的影视创作论坛的设计与实现(附:源码 论文 sql文件 项目部署教程)
Persistence practice of redis (Linux version)
Intel distiller Toolkit - Quantitative implementation 1
Blue Bridge Cup_ Single chip microcomputer_ PWM output
Sentinel mode of redis
随机推荐
工作流—activiti7环境搭建
Kratos战神微服务框架(二)
Go redis initialization connection
Publish and subscribe to redis
在QWidget上实现窗口阻塞
LeetCode:124. Maximum path sum in binary tree
QML control type: Popup
【shell脚本】——归档文件脚本
Advanced Computer Network Review(4)——Congestion Control of MPTCP
Post training quantification of bminf
IDS' deletion policy
Redis geospatial
Blue Bridge Cup_ Single chip microcomputer_ Measure the frequency of 555
IDS cache preheating, avalanche, penetration
Advanced Computer Network Review(3)——BBR
KDD 2022论文合集(持续更新中)
Global and Chinese markets for modular storage area network (SAN) solutions 2022-2028: Research Report on technology, participants, trends, market size and share
[daily question] Porter (DFS / DP)
Kratos战神微服务框架(三)
Basic usage of xargs command