当前位置:网站首页>LeetCode Review Diary: 34. Find the first and last position of an element in a sorted array
LeetCode Review Diary: 34. Find the first and last position of an element in a sorted array
2022-08-02 01:54:00 【light [email protected]】
给你一个按照非递减顺序排列的整数数组 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;
}
}
版权声明
本文为[light [email protected]~no trace]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/214/202208020144011761.html
边栏推荐
- Win Go开发包安装配置、GoLand配置
- HSDC和独立生成树相关
- AOF重写
- typescript30-any类型
- Yunhe Enmo: Let the value of the commercial database era continue to prosper in the openGauss ecosystem
- 电商库存系统的防超卖和高并发扣减方案
- 电子制造仓储条码管理系统解决方案
- Entry name ‘org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt’ collided
- 大话西游无法登陆解决
- Image fusion based on weighted 】 and pyramid image fusion with matlab code
猜你喜欢
The ultra-large-scale industrial practical semantic segmentation dataset PSSL and pre-training model are open source!
typescript33 - high-level overview of typescript
Reflex WMS中阶系列6:对一个装货重复run pick会有什么后果?
6-25 Vulnerability Exploitation - irc Backdoor Exploitation
FlinkSQL CDC实现同步oracle数据到mysql
Some insights from 5 years of automated testing experience: UI automation must overcome these 10 pits
typescript35-class的构造函数
大话西游无法登陆解决
【服务器数据恢复】服务器Raid5阵列mdisk磁盘离线的数据恢复案例
volatile原理解析
随机推荐
华为5年女测试工程师离职:多么痛的领悟...
当关注「互联网+」模式的时候,通常仅仅只是在关注「互联网+」模式本身
typescript34-class的基本使用
The ultra-large-scale industrial practical semantic segmentation dataset PSSL and pre-training model are open source!
LeetCode刷题日记:153、寻找旋转排序数组中的最小值
typescript30 - any type
Use flex-wrap to wrap lines in flex layout
GO开发环境配置
Typescript31 - any type
Win Go开发包安装配置、GoLand配置
Byte taught me a hard lesson: When a crisis comes, you don't even have time to prepare...
Kubernetes — 核心资源对象 — 网络
哈希冲突和一致性哈希
Flink_CDC搭建及简单使用
MySQL优化策略
第一次写对牛客的编程面试题:输入一个字符串,返回该字符串出现最多的字母
Flink_CDC construction and simple use
Huawei's 5-year female test engineer resigns: what a painful realization...
密码学的基础:X.690和对应的BER CER DER编码
使用百度EasyDL实现厂区工人抽烟行为识别