当前位置:网站首页>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;
}
}
边栏推荐
- 哈希表
- 3.Bean的作用域与生命周期
- Entry name ‘org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt’ collided
- canal实现mysql数据同步
- 编码经验之谈
- Oracle data to mysql FlinkSQL CDC to achieve synchronization
- MySQL——增删查改操作
- Two ways to pass feign exceptions: fallbackfactory and global processing Get server-side custom exceptions
- 电子制造仓储条码管理系统解决方案
- 【ORB_SLAM2】void Frame::AssignFeaturesToGrid()
猜你喜欢

27英寸横置大屏+实体按键,全新探险者才是安全而合理的做法!

Image fusion based on weighted 】 and pyramid image fusion with matlab code

使用百度EasyDL实现厂区工人抽烟行为识别

大话西游创建角色失败解决

手写一个博客平台~第三天

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

Yunhe Enmo: Let the value of the commercial database era continue to prosper in the openGauss ecosystem

typescript34-class的基本使用

typescript33 - high-level overview of typescript

外包干了三年,废了...
随机推荐
Detailed explanation of fastjson
feign异常传递的两种方式 fallbackfactory和全局处理 获取服务端自定义异常
5年自动化测试经验的一些感悟:做UI自动化一定要跨过这10个坑
Redis 持久化 - RDB 与 AOF
Image fusion based on weighted 】 and pyramid image fusion with matlab code
Yunhe Enmo: Let the value of the commercial database era continue to prosper in the openGauss ecosystem
GO开发环境配置
typescript33 - high-level overview of typescript
Huawei's 5-year female test engineer resigns: what a painful realization...
Redis 订阅与 Redis Stream
6-24 exploit-vnc password cracking
S/4中究竟有多少个模块,你对这些模块了解多少
哈希表
Kubernetes之本地存储
Kubernetes — 网络流量模型
typescript31-any类型
6-24漏洞利用-vnc密码破解
安全(1)
Kubernetes — 核心资源对象 — Controller
typescript35-class的构造函数