当前位置:网站首页>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
边栏推荐
- 秒懂大模型 | 3步搞定AI写摘要
- Kubernetes — 核心资源对象 — 存储
- Reflex WMS中阶系列7:已经完成拣货尚未Load的HD如果要取消拣货,该如何处理?
- Entry name ‘org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt’ collided
- 数据链路层的数据传输
- Oracle data to mysql FlinkSQL CDC to achieve synchronization
- 使用百度EasyDL实现厂区工人抽烟行为识别
- 雇用WordPress开发人员:4个实用的方法
- typescript34-class的基本使用
- GO开发环境配置
猜你喜欢

typescript31-any类型

Huawei's 5-year female test engineer resigns: what a painful realization...

『网易实习』周记(二)

Entry name ‘org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt’ collided

6-24漏洞利用-vnc密码破解

力扣 1374. 生成每种字符都是奇数个的字符串

检查IP或端口是否被封

volatile原理解析

FlinkSQL CDC实现同步oracle数据到mysql

Flask gets post request parameters
随机推荐
¶Backtop 回到顶部 不生效
记录一次数组转集合出现错误的坑点,尽量使用包装类型数组进行转换
Day116.尚医通:预约挂号详情 ※
volatile原理解析
成都openGauss用户组招募啦!
Rust P2P Network Application Combat-1 P2P Network Core Concepts and Ping Program
手写一个博客平台~第一天
Kubernetes — 网络流量模型
【Brush the title】Family robbery
"Introduction to Natural Language Processing Practice" Question Answering Robot Based on Knowledge Graph
『网易实习』周记(一)
Named parameter implementation of JDBC PreparedStatement
数据链路层的数据传输
PHP 使用 PHPRedis 与 Predis
制造企业数字化转型现状分析
Reflex WMS中阶系列7:已经完成拣货尚未Load的HD如果要取消拣货,该如何处理?
当关注「互联网+」模式的时候,通常仅仅只是在关注「互联网+」模式本身
AntPathMatcher使用
For effective automated testing, these software testing tools must be collected!!!
The ultra-large-scale industrial practical semantic segmentation dataset PSSL and pre-training model are open source!