当前位置:网站首页>[二分查找中等题] 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;
}
边栏推荐
- Interview shock 68: why does TCP need three handshakes?
- 函数栈帧详解
- Leetcode- > binary search clock in
- Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
- Applet utils
- iNFTnews | GGAC联合中国航天ASES 独家出品《中国2065典藏版》
- 听说你们面试都跪在了接口测试上?
- 软件测试相关试题知识点
- Database knowledge required by testers: MySQL common syntax
- 阿里云解决方案架构师张平:云原生数字化安全生产的体系建设
猜你喜欢

Dynamically set the height of applet swiper

论构造函数的原型是谁

杀毒软件 clamav 的安装和使用

百度云人脸识别

Cloud development sleeping alarm clock wechat applet source code
![[nisactf 2022] upper](/img/61/05291ba7a63fe13882e49ab026df14.png)
[nisactf 2022] upper

MySQL master-slave database configuration based on docker for Ubuntu

How to do the system security test? Let's talk about it in detail

八皇后编程实现

哈希表与一致性哈希的原理理解以及应用
随机推荐
手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践
Web3.0世界知识体系分享-什么是Web3.0
Interrupt, signal, system call
iNFTnews | GGAC联合中国航天ASES 独家出品《中国2065典藏版》
Dynamically set the height of applet swiper
shell(38) : ssh端口转发
Database read-write separation and database and table segmentation
数据库读写分离和分库分表
百度云人脸识别
As for the pit saved by serialized variables, the data added with indexer cannot be serialized
Uni app wechat applet search keywords are displayed in red
确定了,2022下半年软考报名8月开始
用最原始的方法纯手工实现常见的 20 个数组方法
C language program compilation
Sort icons with swiper
MySQL 5.7 takes the first item of the group
小程序怎样助力智能家居生态新模式
ArduinoUNO驱动RGB模块全彩效果示例
Debezium系列之:记录从库服务器挂掉后binlog文件无法恢复,任务切换到主库并保证数据不丢失的方法
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议