当前位置:网站首页>LeetCode刷题日记:34、 在排序数组中查找元素的第一个和最后一个位置
LeetCode刷题日记:34、 在排序数组中查找元素的第一个和最后一个位置
2022-08-02 01:44:00 【淡墨@~无痕】
给你一个按照非递减顺序排列的整数数组 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]
提示:
0 <= nums.length <= 105
-109 <= nums[i] <= 109
nums 是一个非递减数组
-109 <= target <= 109
方法1:
class Solution {
public int[] searchRange(int[] nums, int target) {
int start = -1, end = -1;
int i = 0, j = nums.length-1;
if(nums.length == 0 || nums == null){
return new int[]{
start, end};
}
while (i <= j){
if(target != nums[i]){
i++;
}else if(target != nums[j]){
j--;
}else if(target == nums[i] && target == nums[j]){
start = i;
end = j;
break;
}
}
return new int[]{
start,end};
}
}
方法2:
class Solution {
public int[] searchRange(int[] nums, int target) {
int start = -1, end = -1;
int i = 0, j = nums.length-1;
while (i <= j){
if(target != nums[i]){
i++;
}else if(target != nums[j]){
j--;
}else if(target == nums[i] && target == nums[j]){
start = i;
end = j;
break;
}
}
return new int[]{
start,end};
}
}
方法3:
class Solution {
public int[] searchRange(int[] nums, int target) {
int a = -1,b=-1,c=0;
for(int i=0;i<nums.length;i++){
if(nums[i]==target){
if(c==0){
a = i;
c = 1;
}
b = i;
}
}
return new int[] {
a,b};
}
}
官方题解:二分算法
class Solution {
public int[] searchRange(int[] nums, int target) {
int leftIdx = binarySearch(nums, target, true);
int rightIdx = binarySearch(nums, target, false) - 1;
if (leftIdx <= rightIdx && rightIdx < nums.length && nums[leftIdx] == target && nums[rightIdx] == target) {
return new int[]{
leftIdx, rightIdx};
}
return new int[]{
-1, -1};
}
public int binarySearch(int[] nums, int target, boolean lower) {
int left = 0, right = nums.length - 1, ans = nums.length;
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] > target || (lower && nums[mid] >= target)) {
right = mid - 1;
ans = mid;
} else {
left = mid + 1;
}
}
return ans;
}
}
边栏推荐
- Kubernetes — 核心资源对象 — 存储
- FlinkSQL CDC实现同步oracle数据到mysql
- 【服务器数据恢复】服务器Raid5阵列mdisk磁盘离线的数据恢复案例
- 乱七八糟的网站
- Reflex WMS中阶系列7:已经完成拣货尚未Load的HD如果要取消拣货,该如何处理?
- Image fusion based on weighted 】 and pyramid image fusion with matlab code
- Can‘t connect to MySQL server on ‘localhost3306‘ (10061) 简洁明了的解决方法
- go版本升级
- 【ORB_SLAM2】void Frame::AssignFeaturesToGrid()
- 超大规模的产业实用语义分割数据集PSSL与预训练模型开源啦!
猜你喜欢
ofstream,ifstream,fstream读写文件
6-24漏洞利用-vnc密码破解
Day116. Shangyitong: Details of appointment registration ※
typescript30 - any type
【轮式里程计】
Constructor instance method of typescript36-class
Flask gets post request parameters
Image fusion based on weighted 】 and pyramid image fusion with matlab code
flask获取post请求参数
Day116.尚医通:预约挂号详情 ※
随机推荐
Reflex WMS中阶系列6:对一个装货重复run pick会有什么后果?
PHP 使用 PHPRedis 与 Predis
信息化和数字化的本质区别是什么?
6-24漏洞利用-vnc密码破解
Entry name ‘org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt’ collided
Kubernetes — Flannel
Kubernetes之本地存储
After graduating from three books, I was rejected by Tencent 14 times, and finally successfully joined Alibaba
Navicat data shows incomplete resolution
内部类、异常简单介绍(第十天)
Detailed explanation of fastjson
flex布局中使用flex-wrap实现换行
3. Bean scope and life cycle
【ORB_SLAM2】void Frame::ComputeImageBounds(const cv::Mat &imLeft)
For effective automated testing, these software testing tools must be collected!!!
Day116.尚医通:预约挂号详情 ※
设备树学习
电商库存系统的防超卖和高并发扣减方案
Record the pits where an error occurs when an array is converted to a collection, and try to use an array of packaging types for conversion
R语言使用cph函数和rcs函数构建限制性立方样条cox回归模型、使用anova函数进行方差分析通过p值确认指定连续变量和风险值HR之间是否存在非线性关系