[LeetCode] (medium) 34. Find First and Last Position of Element in Sorted Array

【Leetcode】34. 在排序数组中查找元素的第一个和最后一个位置(Find First and Last Position of Element in Sorted Array) Leetcode - 34 Find First and Last Position of Element in Sorted Array (Medium) 题目描述:给定一个有序序列与一个 target 值,求出 target 在有序序列中的区间,时间复杂度要求为 O(logn),若没有 target 值,则返回 [-1, -1]。 Input: nums = [5,7,7,8,8,10], t... 阅读详情

https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/

Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.

Your algorithm's runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

Example 1:

Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]

Example 2:

Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]

 二分搜索上下界,用的邓俊辉的理解方法,很直观。

搜索上界时,hi及hi的右侧满足>target,lo的左侧满足<=target,因此当lo==hi时,lo-1即为target应在位置的上界

搜索下界时,lo及lo的左侧满足<target,hi的右侧满足>=target,因此当lo==hi时,hi+1即为target应在位置的下界

注意在搜索下界时要确保每一次迭代问题的潜在解空间会缩小,即每一步都必须要移动两侧边界,所以mid = (lo+hi+1)/2

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        int hi = search_up(nums, target);
        if(hi == -1 || nums[hi] != target) return {-1, -1};
        int lo = search_down(nums, target);
        return {lo, hi};
    }
    
    int search_up(vector<int>& nums, int target){
        int lo = 0;
        int hi = nums.size();
        int mid;
        while(lo < hi){
            mid = (lo+hi)/2;
            if(nums[mid] > target) hi = mid;
            else lo = mid+1;
        }
        return lo-1;
    }
    
    int search_down(vector<int>& nums, int target){
        int lo = -1;
        int hi = nums.size()-1;
        int mid;
        while(lo < hi){
            mid = (lo+hi+1)/2;
            if(nums[mid] < target) lo = mid;
            else hi = mid-1;
        }
        return lo+1;
    }
};

 

2024广东省职业技能大赛云计算——集群部署GitLab 题目是这样的:在Kubernetes集群中新建命名空间gitlab-ci,将GitLab部署到该命名空间下,Deployment和Service名称均为gitlab,以NodePort方式将80端口对外暴露为30880,设置GitLab服务root用户的密码为admin@123,将项目包demo-2048.tar.gz导入到GitLab中并命名为demo-2048。GitLab是一个功能强大且灵活的 DevOps 平台,为开发团队和企业提供了从代码管理到持续集成、交付和部署的一整套解决方案。 阅读详情

相关推荐

Cadence IC618/Spectre231安装避坑指南:详解环境变量配置与License管理那些事儿

本文深入解析Cadence IC618/Spectre231安装过程中的环境变量配置与License管理难题,提供从变量冲突解决到高效License管理的实用方案。针对常见的64位标识冲突、路径污染及License服务器连接问题,给出具体诊断步骤和优化建议,帮助用户构建稳定可靠的EDA环境。

weixin_33737134的博客 616

[LeetCode] 34. Find First and Last Position of Element in Sorted Array Medium

Find First and Last Position of Element in Sorted Array Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. Your algorithm’s runti...

Peng_maple的博客 328

Labelme安装教程(附Anaconda安装教程)

本文详细介绍了如何安装Anaconda和Labelme,还提供了常见问题的解决建议。

HanYan_ing的博客 1万+

leetcode 34. Find First and Last Position of Element in Sorted Array (medium)

搜索时间复杂度要求O(log(n)),又是有序的数组,所以用二分搜索。 class Solution { public: vector<int> searchRange(vector<int> &nums, int target) { vector<int> res(2, -1); if (nums.size() == 0) retur...

Rh's blog 253

告别卡顿!用AVPro Video在Unity 2022.3里流畅播放4K视频的保姆级配置流程

本文详细介绍了如何在Unity 2022.3中使用AVPro Video插件实现4K视频的流畅播放。从环境配置、插件选型到核心性能优化技巧,包括解码器设置、内存管理策略和多平台适配方案,帮助开发者彻底解决卡顿问题,提升视频播放体验。

weixin_32349561的博客 318

RFSoC应用XCZU47DR八发八收FPGA设计方案

本文介绍了基于Xilinx XCZU47DR RFSoC芯片的八发八收FPGA设计方案。该方案采用集成射频数据转换器的SoC架构,支持8路14bit ADC(5GSPS)和8路14bit DAC(9.85GSPS),可直接处理5GHz以下射频信号。硬件设计包含高性能输入缓冲器、QMC校正和复杂混频器,适用于5G/6G通信、雷达、卫星通信等领域。文章详细阐述了ADC/DAC子系统参数,包括通道配置、采样率、带宽等特性,并提供了FPGA顶层模块的硬件接口设计,展示了多通道数据收发处理能力。该方案无需外置混频器,

toonyhe的博客 1786

Leetcode 34. Find First and Last Position of Element in Sorted Array [medium][java]

Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of O(log n). If the t...

inkhk的博客 358

Allegro如何输出ODB文件操作指导

在PCB设计完成之后,需要输出生产文件用于生产加工,除了gerber文件可以用生产制造,ODB文件同样也可以用于生产,如下图。用Allegro如何输出ODB文件,具体操作如下。如何输出ODB文件操作指导。

weixin_54787054的博客 7203

【LeetCode】 34. Find First and Last Position of Element in Sorted Array (Medium)(JAVA)

【LeetCode】 34. Find First and Last Position of Element in Sorted Array 在排序数组中查找元素的第一个和最后一个位置(Medium)(JAVA) 题目地址: https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/...

qq_16927853的博客 246

【GameFramework框架内置模块】2、数据节点(Data Node)

这一篇讲数据节点DataNode,基本开发程序都会有类似数据节点作用的东西,数据节点的作用其实就是保存游戏运行时的一些缓存数据,比如用户登录信息、用户名、任务节点等等。数据一般会有增删改查这四种操作,在运行时的数据一般是使用单例或者静态属性,将数据存储在内存中而数据节点也是将数据存储在内存中,但是不用序列化数据,比使用单例或者静态属性要容易维护的多,特别时它的树形结构可以让我们很好的组织我们的数据结构,方便做增删改查操作。数据节点DataNode模块的数据的存储方式是以树形结构存储。

优快云博客专家,Unity3D高级软件工程师,《Unity 3D从入门到实战》作者。 3184

【数据集】中国陆地生态系统通量观测网 ChinaFLUX

【数据集】中国陆地生态系统通量观测网 ChinaFLUX

WW、forever的博客 188

【python3】leetcode 34. Find First and Last Position of Element in Sorted Array(Medium)

34. Find First and Last Position of Element in Sorted Array(Medium) Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. Your al...

momottyy的专栏 390

西门子SMART PLC增量式PID(完整梯形图FC)

西门子PLC自带的PID指令是基于位置型的PID,位置型PID的算法公式和详细介绍,大家可以参看《PID专栏》的其它文章,这里不再赘述。本篇博文直接给出代码部分的讲解,不设计太多公式和理论的推导。限于本人能力,文中难免出现错误和不足之处,诚恳欢迎大家的批评和指正。同时感谢大家的订阅和关注。SMART200 PLC系统自带的PID指令解析,可以参看下面这篇文章:SMART PLC PID算法基本解析(附公式)_RXXW_Dor的博客-优快云博客_plc的pid算法在态运行中,控制器调节输出值,使偏差 (e

RXXW_Dor的博客 8147

LeetCode_Medium_34. Find First and Last Position of Element in Sorted Array

2019.2.12 题目描述: Given an array of integers nums sorted in ascending order, find the starting and ending position of a given targetvalue. Your algorithm's runtime complexity must be in the order of ...

weixin_41637618的博客 275

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

LeetCode Top 100 Liked Questions 34. Find First and Last Position of Element in Sorted Array(Medium)

welcome to my blog LeetCode Top 100 Liked Questions 34. Find First and Last Position of Element in Sorted Array(Java版; Medium) 题目描述 Given an array of integers nums sorted in ascending order, find the ...

littlehaes的博客 147

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

【leetcode】34. (Medium)Find First and Last Position of Element in Sorted Array(JAVA)

题目链接 解题思路: 这道题的意思是找到数组里面一个数的起始位置和末尾位置,比如: [1,5,5,7,7,8] 8的起始末尾位置为[5,5] 如果没有这个目标数,就返回[-1,-1] 还是折半查找做,先看能不能找到这个数,能找到就设置两个指针start,end从这个数开始分别向两边外拓;如果过没有找到返回[-1,-1] 提交代码: class Solution { public in...

AXIMI的博客 179

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

【LeetCode】解题34:Find First and Last Position of Element in Sorted Array

LeetCode解题 34:Find First and Last Position of Element in Sorted Array Problem 34: Find First and Last Position of Element in Sorted Array [Medium]解题思路Solution (Java) Problem 34: Find First and Last Po...

Fayedily的博客 207

leetcode 34. Find First and Last Position of Element in Sorted Array【Java】

题目描述 在排序数组中查找元素的第一个和最后一个位置 AC代码 class Solution { public int[] searchRange(int[] nums, int target) { int []res={-1,-1}; if(nums==null||nums.length==0) return res; ...

NayelyA的博客 288

【小白爬Leetcode34】6.1 在排序数组中查找元素的第一个和最后一个位置 Find First and Last Position of Element in Sorted Array

【小白爬Leetcode34】6.1 在排序数组中查找元素的第一个和最后一个位置 Find First and Last Position of Element in Sorted Array题目Discription中文描述思路 两次二分查找:实现一:实现二 Leetcode34 medium\color{#FF4500}{medium}medium 点击进入原题链接:Leetcode34 在排序数组中查找元素的第一个和最后一个位置 Find First and Last Position of E

weixin_41604040的博客 318

LeetCode34: Find First and Last Position of Element in Sorted Array

1 问题 问题描述(原文): Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. If target is not found in the array, return [-1, -1]. You must write an algorithm with O(log n) runtime complexity. Exa

四年时光 127

LeetCode 34. Find First and Last Position of Element in Sorted Array

LeetCode 34. Find First and Last Position of Element in Sorted Array 题目描述: 给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。 你的算法时间复杂度必须是 O(log n) 级别。 如果数组中不存在目标值,返回 [-1, -1]。 示例 1: 输入: nums =...

qq_30122883的博客 147

leetcode -- 34. Find First and Last Position of Element in Sorted Array

题目描述 题目难度:Medium Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of O...

tkzc_csk的博客 203

[LeetCode] (medium) 33. Search in Rotated Sorted Array

https://leetcode.com/problems/search-in-rotated-sorted-array/ Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,1,2,4,5,6,7] might become [4,5...

Frontier_Setter的博客 757

[LeetCode] (medium) 322. Coin Change

https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to ...

Frontier_Setter的博客 747

[LeetCode] (medium) 416. Partition Equal Subset Sum

https://leetcode.com/problems/partition-equal-subset-sum/ Given anon-emptyarray containingonly positive integers, find if the array can be partitioned into two subsets such that the sum of elemen...

Frontier_Setter的博客 366

[LeetCode] (medium) 240. Search a 2D Matrix II

https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are so...

Frontier_Setter的博客 336
上一篇: [LeetCode] (medium) 33. Search in Rotated Sorted Array
下一篇: [LeetCode] (medium) 46. Permutations
Frontier_Setter
博客等级 码龄10年 2粉丝 123原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值