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

【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... 阅读详情

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]

解析

在排序数组中找一个数的范围,即左边界和右边界。
定义两个找最左边界和最右边界的函数,在左边界中,如果找到mid == target,判断mid==0 或者mid前一个是否等于target,如果不等于,则找到,如果等于,则继续二分查找

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        int left = GetfirstK(nums, target);
        int right = GetlastK(nums, target);
        vector<int> res;
        res.push_back(left);
        res.push_back(right);
        return res;
    }
    
    int GetfirstK(vector<int>& nums, int target){
        int left = 0,right = nums.size()-1;
        while(left<=right){
            int mid = (left+right)>>1;
            if(nums[mid] == target){
                if(mid == 0 || (mid>0 && nums[mid-1] != target))
                    return mid;
                else
                    right = mid-1;
            }
            else if(nums[mid] > target)
                right = mid-1;
            else
                left = mid+1;
        }
        return -1;
    }
    
    int GetlastK(vector<int>& nums, int target){
        int size = nums.size();
        int left = 0,right = nums.size()-1;
        while(left<=right){
            int mid = (left+right)>>1;
            if(nums[mid] == target){
                if(mid == size-1 || (mid <size-1 && nums[mid+1] != target))
                    return mid;
                else
                    left = mid+1;
            }
            else if(nums[mid] > target)
                right = mid-1;
            else
                left = mid+1;
        }
        return -1;
    }
};

也可以在一个函数中找到两个值,具体参考

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

参考

http://www.cnblogs.com/grandyang/p/4409379.html

在thinkBook16的win11基础上安装ubuntu22.04 背景:联想ThinkBook16 pro 32G/512G,系统盘已安装Win11,加装一条M.2-2T固态,想在2T上分一个300GB的EXT4分区,然后把ubuntu22.04安装在该分区上,并实现Win11+Linux多系统共存。 目标:实现多磁盘下分区管理的简化,优化掉一些不必要的多余的维护性质的分区。 阅读详情

相关推荐

Burp靶场实战:JWT签名绕过漏洞分析与利用

本文通过Burp Suite靶场实战,深入剖析了JWT签名绕过漏洞的多种攻击手法,包括签名未验证、alg:none攻击、弱密钥爆破、JWK/JKU头注入、KID路径遍历及算法混淆攻击。文章详细演示了利用过程,并提供了关键的防御指南,帮助开发和安全人员加固JWT实现,确保认证安全。

weixin_29325515的博客 596

【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

EBS开发_离散工单创建API

--以下代码于20171213在TONY环境中实现 DECLARE     l_iface_rec    wip.wip_job_schedule_interface%ROWTYPE; --工单任务接口表     l_group_id     NUMBER;     l_returnstatus VARCHAR2(40);     l_errormsg     VARCHAR2(2000

蜗牛先生快跑的博客 2596

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

c#笔记之mes上传Http协议版

HTTP(超文本传输协议) 是一种典型的协议。

qq_61441787的博客 346

Dify 从入门到熟悉100 天的学习大纲

理解 Dify 的核心概念,熟悉操作界面,并能创建简单的 AI 应用。祝你在这 100 天的旅程中学有所成,成为一名优秀的 AI 应用开发者!掌握生产环境部署、监控、调优,并关注 Dify 生态和前沿动态。探索多模态、Agent、API 集成、性能优化等高级主题。掌握工作流编排、高级提示词技巧、知识库优化等核心功能。

IT深耕十余载,大道之简 1735

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

PCL 直通滤波器【2026最新版】

PCL直通滤波器函数详解以及用于多字段滤波、强度滤波、获取某一时间范围内的点等高阶用法案例分享。博客长期更新,本文最近一次更新时间为:2026年3月11日。

点云侠的博客 40万+

【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

从 Claude Code 到 SAP ADT,vibing-steampunk 把 ABAP 开发带进 Agentic 工程现场

vibing-steampunk(vsp)是一个连接SAP ADT API与AI编码代理的桥梁工具,通过MCP协议实现ABAP开发的自动化。它绕开传统GUI脚本方式,直接利用ADT REST API进行代码读写、调试和部署。该工具提供包分析、边界检查、LUW分析等功能,并创新性地采用Hyperfocused模式和方法级编辑来优化token使用效率。通过上下文压缩技术,vsp能智能提取关键代码片段,显著提升AI处理ABAP代码的精准度,为SAP系统现代化改造提供了高效的技术路径。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 190

Android15 修改触摸点上限为40

把MAX_POINTER_ID从默认的31修改为39,这里为什么要从31修改为39,是因为上面说明用BitSet32极限就只能支持32个点,因为从最小的ID是0,所以要支持40个点ID也要对应改为39.最近有个需求,需要修改Android系统的触摸点数,Android系统默认的点数是16个点,因为现在是大屏项目,有客户需要修改为40个点,好吧,客户最大。6.最后进行编译,开机开启触摸点检测,可以使用开发者模式中的显示触摸点功能,再叫上3个人,用双掌触摸屏幕,发现左上角有显示40个触摸点,修改成功。

tzj1058756809的博客 573

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

提示工程从初级到高级:如何评估提示词的鲁棒性?架构师分享测试方法

鲁棒性(Robustness)原本是工程领域的概念,指系统在“非理想条件”下保持性能稳定的能力。输入鲁棒性:面对不规范、有噪音、格式变异的用户输入,提示能否引导模型输出正确结果?模型鲁棒性:同一提示在不同大模型(或同一模型的不同版本)下,输出是否一致?场景鲁棒性:在边界场景(极端case、歧义case、多任务混合)下,提示能否保持逻辑一致性?提示词V1:请将以下用户评论分类为「正面」「中性」或「负面」:{comment}。要求:输出唯一类别,不要额外说明。

小程序开发 496

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

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 tar...

Frontier_Setter的博客 148

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

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

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

【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

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

【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] 221. Maximal Square && 85. Maximal Rectangle

Maximal Square Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area. Example: Input: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Output: 4 解法...

Peng_maple的博客 347

[LeetCode] 17. Letter Combinations of a Phone Number

Letter Combinations of a Phone Number Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A mapping of digit to letters (just ...

Peng_maple的博客 325

[leetcode] 139. Word Break

Word Break Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. N...

Peng_maple的博客 280

[leetcode] 97. Interleaving String

Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = “aabcc”, s2 = “dbbca”, s3 = “aadbbcbcac” Output: true Example 2: Input: s1 =...

Peng_maple的博客 278
上一篇: [LeetCode] 33. Search in Rotated Sorted Array
下一篇: [LeetCode] 37. Sudoku Solver
Peng_maple
博客等级 码龄10年 0粉丝 79原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值