当前位置:网站首页>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;
}
}
二、总结
边栏推荐
- postman之参数化详解
- Selenium+pytest automated test framework practice
- QML type: overlay
- Kratos战神微服务框架(一)
- Advanced Computer Network Review(3)——BBR
- Post training quantification of bminf
- QML control type: menu
- LeetCode:34. Find the first and last positions of elements in a sorted array
- Nacos installation and service registration
- Intel distiller Toolkit - Quantitative implementation 3
猜你喜欢
IDS' deletion policy
Kratos ares microservice framework (I)
Redis之五大基础数据结构深入、应用场景
Redis cluster
Servlet learning diary 8 - servlet life cycle and thread safety
Redis connection redis service command
Kratos ares microservice framework (II)
Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
[oc]- < getting started with UI> -- common controls uibutton
postman之参数化详解
随机推荐
go-redis之初始化连接
Intel distiller Toolkit - Quantitative implementation 1
Pytest's collection use case rules and running specified use cases
LeetCode:124. Maximum path sum in binary tree
Leetcode: Jianzhi offer 04 Search in two-dimensional array
[oc foundation framework] - < copy object copy >
Redis connection redis service command
【图的三大存储方式】只会用邻接矩阵就out了
Sentinel mode of redis
LeetCode:26. Remove duplicates from an ordered array
Global and Chinese market of appointment reminder software 2022-2028: Research Report on technology, participants, trends, market size and share
Kratos ares microservice framework (III)
[OC foundation framework] - string and date and time >
Redis之主从复制
KDD 2022 paper collection (under continuous update)
CSP salary calculation
[oc]- < getting started with UI> -- common controls uibutton
BN folding and its quantification
[three storage methods of graph] just use adjacency matrix to go out
基于B/S的网上零食销售系统的设计与实现(附:源码 论文 Sql文件)