当前位置:网站首页>[二分查找中等题] LeetCode 34. 在排序数组中查找元素的第一个和最后一个位置
[二分查找中等题] LeetCode 34. 在排序数组中查找元素的第一个和最后一个位置
2022-07-27 00:21:00 【哇咔咔负负得正】
LeetCode 34. 在排序数组中查找元素的第一个和最后一个位置
https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/comments/
给你一个按照非递减顺序排列的整数数组 nums,和一个目标值 target。请你找出给定目标值在数组中的开始位置和结束位置。
如果数组中不存在目标值 target,返回 [-1, -1]。
你必须设计并实现时间复杂度为 O(log n) 的算法解决此问题。
示例 1:
输入:nums = [5,7,7,8,8,10], target = 8
输出:[3,4]
示例 2:
输入:nums = [5,7,7,8,8,10], target = 6
输出:[-1,-1]
示例 3:
输入:nums = [], target = 0
输出:[-1,-1]
Solution
二分 + 向左向右遍历(Java),时间复杂度: O ( l o g n + n ) O(log n + n) O(logn+n):
class Solution {
public int[] searchRange(int[] a, int target) {
int[] ans = {
-1, -1};
int l = 0, r = a.length - 1, mid = 0;
// 先二分找到 target 对应的数
while (l <= r) {
mid = (l + r) / 2;
if (a[mid] == target) {
ans[0] = mid;
ans[1] = mid;
break;
} else if (a[mid] < target) {
l = mid + 1;
} else {
r = mid - 1;
}
}
// 向 target 左面和右面分别进行遍历找到等于 target 的最小索引和最大索引
for (int i = mid - 1; i >= 0; i--) {
if (a[i] != target) {
break;
}
ans[0] = i;
}
for (int i = mid + 1; i < a.length; i++) {
if (a[i] != target) {
break;
}
ans[1] = i;
}
return ans;
}
}
二分找最大最小下标(C) ,时间复杂度: O ( l o g n ) O(log n) O(logn) :searchInsert() 函数直接 Copy 之前写过的 LeetCode 35. 搜索插入位置的代码,该代码会找到等于 target 值的最小下标。
int searchInsert(int* a, int n, int target){
int l = 0, r = n - 1;
int ans = n;
int mid;
while (l <= r) {
mid = (l + r) / 2;
if (a[mid] >= target) {
ans = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return ans;
}
int* searchRange(int* nums, int numsSize, int target, int* returnSize){
int *reslut = (int*) malloc(sizeof(int) * 2);
*returnSize = 2; // 这个必须加,不加报错
reslut[0] = reslut[1] = -1;
// 找插入 target 位置的最小索引
int left_idx = searchInsert(nums, numsSize, target);
// 若最小下标小于表长并且找到的最小索引对应的数 == target
if (left_idx < numsSize && nums[left_idx] == target) {
reslut[0] = left_idx;
// 寻找插入 (target + 1) 最小索引值 - 1
// (target + 1) 的最小索引值 - 1 表示等于 target 的最大索引值
reslut[1] = searchInsert(nums, numsSize, target + 1) - 1;
}
return reslut;
}
边栏推荐
- Which securities firm is safer to open an account and buy REITs funds?
- How to do the system security test? Let's talk about it in detail
- Debezium系列之:记录从库服务器挂掉后binlog文件无法恢复,任务切换到主库并保证数据不丢失的方法
- If you want to thoroughly optimize the performance, you must first understand the underlying logic~
- 次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
- {“errcode“:44001,“errmsg“:“empty media data, hint: [1655962096234893527769663], from ip: 222.72.xxx.
- I heard that you knelt on the interface test during the interview?
- F8 catch traffic, F9 catch rabbits, f10turttle
- Interview shock 68: why does TCP need three handshakes?
- [Ryu] common problems and solutions in installing Ryu
猜你喜欢

Goatgui invites you to attend a machine learning seminar

JMeter interface test, quickly complete a single interface request
![[redis] five common data types](/img/0f/17945b6f3a7735d9a569296c2c0e2a.png)
[redis] five common data types

阿里云解决方案架构师张平:云原生数字化安全生产的体系建设
![[NISACTF 2022]上](/img/61/05291ba7a63fe13882e49ab026df14.png)
[NISACTF 2022]上

红宝书第四版的一个错误?

云开发寝适闹钟微信小程序源码

Pyqt5 use pyqtgraph to draw dynamic scatter chart

Talk about connection pools and threads

Favicon网页收藏图标在线制作PHP网站源码/ICO图片在线生成/支持多种图片格式转换
随机推荐
聊聊连接池和线程
Knowledge points of test questions related to software testing
Cookie addition, deletion, modification and query methods
[redis] quick start
信息收集-端口扫描工具-Nmap使用说明
shell(38) : ssh端口转发
用最原始的方法纯手工实现常见的 20 个数组方法
Debezium系列之:记录从库服务器挂掉后binlog文件无法恢复,任务切换到主库并保证数据不丢失的方法
基于.NetCore开发博客项目 StarBlog - (16) 一些新功能 (监控/统计/配置/初始化)
JMeter interface test, quickly complete a single interface request
Kubernetes Dashboard 部署应用以及访问
com.fasterxml.jackson.databind.exc.InvalidDefinitionException
Getlocation:fail the API need to be declared in the requiredprivateinfo field in app.json
白盒测试案例设计(我爷爷都能看懂)
Web3.0世界知识体系分享-什么是Web3.0
快速排序(Quick sort)
Favicon web page collection icon online production PHP website source code /ico image online generation / support multiple image format conversion
JS utils fragmented
Interview shock 68: why does TCP need three handshakes?
如何白嫖最新版BurpSuite Pro