当前位置:网站首页>[binary search medium] leetcode 34. find the first and last positions of elements in the sorted array
[binary search medium] leetcode 34. find the first and last positions of elements in the sorted array
2022-07-27 03:04:00 【Wow, Kaka, negative, positive】
LeetCode 34. Find the first and last positions of the elements in the sort array
https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/comments/
Give you an array of integers in non decreasing order nums, And a target value target. Please find out the start position and end position of the given target value in the array .
If the target value does not exist in the array target, return [-1, -1].
You must design and implement a time complexity of O(log n) The algorithm to solve this problem .
Example 1:
Input :nums = [5,7,7,8,8,10], target = 8
Output :[3,4]
Example 2:
Input :nums = [5,7,7,8,8,10], target = 6
Output :[-1,-1]
Example 3:
Input :nums = [], target = 0
Output :[-1,-1]
Solution
Two points + Traverse left and right (Java), Time complexity : 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;
// Find it by two points first target The corresponding number
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;
}
}
// towards target The left side and the right side are traversed respectively to find equal to target Minimum index and maximum index
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;
}
}
Find the maximum and minimum subscript in two points (C) , Time complexity : O ( l o g n ) O(log n) O(logn) :searchInsert() Function directly Copy It was written before LeetCode 35. Search the code of the insertion location , The code will find equal to target The minimum subscript of the value .
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; // This must be added , No error report
reslut[0] = reslut[1] = -1;
// Find insert target Minimum index of location
int left_idx = searchInsert(nums, numsSize, target);
// If the minimum subscript is less than the table length and the number corresponding to the minimum index found == target
if (left_idx < numsSize && nums[left_idx] == target) {
reslut[0] = left_idx;
// Find insert (target + 1) Minimum index value - 1
// (target + 1) Minimum index value of - 1 Is equal to target Maximum index value for
reslut[1] = searchInsert(nums, numsSize, target + 1) - 1;
}
return reslut;
}
边栏推荐
猜你喜欢

ArduinoUNO驱动RGB模块全彩效果示例

论构造函数的原型是谁

杀毒软件 clamav 的安装和使用

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

White box test case design (my grandfather can understand it)

C language program compilation (preprocessing)

setTimeout第一个参数应该注意的地方

Database read-write separation and database and table segmentation

CuteOne:一款OneDrive多网盘挂载程序/带会员/同步等功能

次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
随机推荐
JMeter interface test, quickly complete a single interface request
CS224W fall 1.2 Applications of Graph ML
Arduinouno drive RGB module full color effect example
使用 WebSocket 实现一个网页版的聊天室(摸鱼更隐蔽)
调用JShaman的Web API接口,实现JS代码加密。
go实现导出excel表格
Thread.Sleep(0)的作用
全网最全的软件测试基础知识整理(新手入门必学)
[redis] quick start
Arduino UNO +74hc164 water lamp example
Debezium系列之:基于debezium offset拉取历史数据,确保数据没有丢失
Shell 分析日志文件命令全面总结
Debezium系列之:记录从库服务器挂掉后binlog文件无法恢复,任务切换到主库并保证数据不丢失的方法
Ten thousand words long text, take you to understand the kubernetes network model
Greed - 376. Swing sequence
Cloud development sleeping alarm clock wechat applet source code
Play a parallel multithreaded mcu-mc3172
vs2019 中编译和使用 protobuf 库
Redis installation and operation (Linux)
论构造函数的原型是谁