当前位置:网站首页>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
边栏推荐
- Why is on-chain governance so important, and how will Polkadot Gov 2.0 lead the development of on-chain governance?
- Entry name ‘org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt’ collided
- Pytorch seq2seq model architecture to achieve English translation tasks
- 百度、百图生科 | HelixFold-Single: 使用蛋白质语言模型作为替代进行无MSA蛋白质结构预测
- 安全(2)
- 有效进行自动化测试,这几个软件测试工具一定要收藏好!!!
- When paying attention to the "Internet +" model, you usually only focus on the "Internet +" model itself
- flex布局中使用flex-wrap实现换行
- Pcie the inbound and outbound
- 3个月测试员自述:4个影响我职业生涯的重要技能
猜你喜欢
『网易实习』周记(二)
typescript33-typescript高级概述
6-24 exploit-vnc password cracking
flask获取post请求参数
Entry name ‘org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt’ collided
typescript33 - high-level overview of typescript
密码学的基础:X.690和对应的BER CER DER编码
typescript34-class的基本使用
Kubernetes之本地存储
typescript30-any类型
随机推荐
密码学的基础:X.690和对应的BER CER DER编码
Redis 持久化 - RDB 与 AOF
【ORB_SLAM2】void Frame::ComputeImageBounds(const cv::Mat &imLeft)
S/4中究竟有多少个模块,你对这些模块了解多少
【轮式里程计】
浅谈国产ERP的“横纵竖”三向发展态势
关于MySQL的数据插入(高级用法)
hash table
传统企业数字化转型需要经过几个阶段?
5年自动化测试经验的一些感悟:做UI自动化一定要跨过这10个坑
字节给我狠狠上了一课:危机来的时候你连准备时间都没有...
Oracle data to mysql FlinkSQL CDC to achieve synchronization
Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读
三本毕业的我被腾讯拒绝了十四次,最终成功入职阿里
LeetCode刷题日记:153、寻找旋转排序数组中的最小值
typescript33 - high-level overview of typescript
Day115. Shangyitong: Background user management: user lock and unlock, details, authentication list approval
typescript36-class的构造函数实例方法
fastjson详解
秒懂大模型 | 3步搞定AI写摘要