当前位置:网站首页>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_07 传智健康项目-Freemarker
- Day_11 传智健康项目-图形报表、POI报表
- 图解 Google V8 # 17:消息队列:V8是怎么实现回调函数的?
- Day_ 05 smart communication health project - appointment management - appointment settings
- MySQL5.6 (5.7-8) 基于shardingsphere5.1.1 Sharding-Proxy模式读写分离
- Day_06 传智健康项目-移动端开发-体检预约
- c#数据库报错问题大家帮我看看吧
- Word pattern for leetcode topic analysis
- Day_03 传智健康项目-预约管理-检查组管理
- Repeated DNA sequences for leetcode topic resolution
猜你喜欢

Summary of business logic security ideas

Day_12 传智健康项目-JasperReports

Day_03 传智健康项目-预约管理-检查组管理
Link of Baidu URL parameter? Research on URL parameter encryption and decryption (code example)

Detailed explanation of redis persistence, master-slave and sentry architecture

Synchronous switching power supply reduces EMI layout dv/dt di/dt

Day_07 传智健康项目-Freemarker

Redis sentry

光谱共焦的测量原理及厚度测量模式

设计师需要懂的数据指标与数据分析模型
随机推荐
Leetcode topic resolution valid Sudoku
业务逻辑安全思路总结
Leetcode topic resolution single number
c#数据库报错问题大家帮我看看吧
Home address exchange
Day_ 09 smart health project - mobile terminal development - Mobile quick login and permission control
什么是客户体验自动化?
如何查看本机IP
Shutter style
Illuminate\Support\Collection 去重 unique 列表去重
How to query fields separated by commas in MySQL as query criteria - find_ in_ Set() function
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
Day_ 04 smart health project - appointment management - package management
[DaVinci developer topic] -41-app how SWC reads and writes NVM block data
【已解决】“The Unity environment took too long to respond. Make sure that :\n“
解析创客教育中的造物原理
Extend your kubernetes API using the aggregation API
Repeated DNA sequences for leetcode topic resolution
Remove the influence of firewall and virtual machine on live555 startup IP address
haas506 2.0開發教程-高級組件庫-modem.sms(僅支持2.2以上版本)