当前位置:网站首页>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
边栏推荐
- 力扣 1161. 最大层内元素和
- GO开发环境配置
- 求大神解答,这种 sql 应该怎么写?
- 传统企业数字化转型需要经过几个阶段?
- Kubernetes — 核心资源对象 — 存储
- 电子制造仓储条码管理系统解决方案
- Fly propeller power space future PIE - Engine Engine build earth science
- Two ways to pass feign exceptions: fallbackfactory and global processing Get server-side custom exceptions
- R语言使用cph函数和rcs函数构建限制性立方样条cox回归模型、使用anova函数进行方差分析通过p值确认指定连续变量和风险值HR之间是否存在非线性关系
- Oracle data to mysql FlinkSQL CDC to achieve synchronization
猜你喜欢
随机推荐
typescript31-any类型
YGG Guild Development Plan Season 1 Summary
go泛型使用方法
canal实现mysql数据同步
ALCCIKERS Shane 20191114
云和恩墨:让商业数据库时代的价值在openGauss生态上持续繁荣
手写一个博客平台~第一天
kubernetes之服务发现
Image fusion based on weighted 】 and pyramid image fusion with matlab code
Anti-oversold and high concurrent deduction scheme for e-commerce inventory system
27英寸横置大屏+实体按键,全新探险者才是安全而合理的做法!
When paying attention to the "Internet +" model, you usually only focus on the "Internet +" model itself
3 Month Tester Readme: 4 Important Skills That Impacted My Career
GO开发环境配置
大话西游无法登陆解决
手写博客平台~第二天
一本适合职场新人的好书
安全(2)
Kubernetes — 核心资源对象 — 网络
大话西游创建角色失败解决









