当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢

The ultra-large-scale industrial practical semantic segmentation dataset PSSL and pre-training model are open source!

FlinkSQL CDC实现同步oracle数据到mysql

Rust P2P Network Application Combat-1 P2P Network Core Concepts and Ping Program

记录一次数组转集合出现错误的坑点,尽量使用包装类型数组进行转换

Understand the big model in seconds | 3 steps to get AI to write a summary

flex布局中使用flex-wrap实现换行

Kubernetes — 核心资源对象 — 存储

【轮式里程计】

Kubernetes — Flannel

Day116.尚医通:预约挂号详情 ※
随机推荐
typescript38-class的构造函数实例方法继承(implement)
云和恩墨:让商业数据库时代的价值在openGauss生态上持续繁荣
使用百度EasyDL实现厂区工人抽烟行为识别
【ORB_SLAM2】SetPose、UpdatePoseMatrices
待读书单列表
PHP 使用 PHPRedis 与 Predis
Flask gets post request parameters
软件测试功能测试全套常见面试题【开放性思维题】面试总结4-3
typescript29-枚举类型的特点和原理
力扣 1374. 生成每种字符都是奇数个的字符串
6-25漏洞利用-irc后门利用
外包干了三年,废了...
超大规模的产业实用语义分割数据集PSSL与预训练模型开源啦!
检查IP或端口是否被封
手写一个博客平台~第三天
AntPathMatcher使用
C语言实验七 二维数组程序设计
Navicat data shows incomplete resolution
Kubernetes之本地存储
A full set of common interview questions for software testing functional testing [open thinking questions] interview summary 4-3