当前位置:网站首页>Topic35——34. Find the first and last positions of elements in a sorted array
Topic35——34. Find the first and last positions of elements in a sorted array
2022-06-23 06:37:00 【_ Cabbage_】
subject : 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]
Tips :
0 <= nums.length <= 105
-109 <= nums[i] <= 109
nums It is a group of non decreasing numbers
-109 <= target <= 109
class Solution {
public int[] searchRange(int[] nums, int target) {
int[] res = new int[2];
Arrays.fill(res, -1);
if(nums.length == 0)
return res;
int l = 0;
int r = nums.length;
// Look for the left border
while(l < r) {
int mid = (l + r) / 2;
if(nums[mid] == target) {
r = mid;
} else if (nums[mid] > target) {
r = mid;
} else if (nums[mid] < target) {
l = mid + 1;
}
if(l >= r && l <= nums.length - 1 && nums[l] == target) {
res[0] = l;
}
}
l = 0;
r = nums.length;
// Look for the right border
while(l < r) {
int mid = (l + r) / 2;
if(nums[mid] == target) {
l = mid + 1;
} else if (nums[mid] > target) {
r = mid;
} else if (nums[mid] < target) {
l = mid + 1;
}
if(l >= r && l - 1 >= 0 && nums[l - 1] == target) {
res[1] = l - 1;
}
}
return res;
}
}
边栏推荐
- Day_ 06 smart health project - mobile terminal development - physical examination appointment
- Docker实战 -- 部署Redis集群与部署微服务项目
- 百度URL参数之LINK?URL参数加密解密研究(代码实例)
- ffplay实现自定义输入流播放
- Machine learning artifact scikit learn minimalist tutorial
- 快速认识 WebAssembly
- Functions and basic structure of CPU
- Day_ 08 smart health project - mobile terminal development - physical examination appointment
- Machine learning 3-ridge regression, Lasso, variable selection technique
- C # database reports errors. Let's have a look
猜你喜欢

Day_ 11 smart communication health project - graphic report and poi Report

Day_ 05 smart communication health project - appointment management - appointment settings

Day_ 01 smart communication health project - project overview and environmental construction

开源生态|超实用开源License基础知识扫盲帖(下)

Day_ 12 smart health project jasperreports

解读创客教育中的团结协作精神

Day_10 传智健康项目-权限控制、图形报表

Day_06 传智健康项目-移动端开发-体检预约

Softing dataFEED OPC Suite将西门子PLC数据存储到Oracle数据库中

Day_01 传智健康项目-项目概述和环境搭建
随机推荐
Leetcode topic resolution valid anagram
How to batch produce QR codes that can be read online after scanning
A review: neural oscillation and brain stimulation in Alzheimer's disease
Summary of business logic security ideas
原址 交换
RF content learning
Golang regular regexp package use -04- use regular replacement (replaceall(), replaceallliteral(), replaceallfunc())
射频内容学习
Docker实战 -- 部署Redis集群与部署微服务项目
haas506 2.0開發教程-高級組件庫-modem.sms(僅支持2.2以上版本)
Day_ 03 smart communication health project - appointment management - inspection team management
百度URL参数之LINK?URL参数加密解密研究(代码实例)
The central network and Information Technology Commission issued the National Informatization Plan for the 14th five year plan, and the network security market entered a period of rapid growth
什么是客户体验自动化?
CPU的功能和基本结构
Machine learning 3-ridge regression, Lasso, variable selection technique
Kubesphere offline deployment without network environment
Remove duplicates from sorted list II of leetcode topic resolution
Dora's Google SEO tutorial (1) SEO novice guide: establishment of preliminary optimization thinking
In the half year summary, it people just want to lie flat