[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

h5页面调用微信JS-SDK分享到微信好友或者朋友圈的配置 1.引入js &lt;script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"&gt;&lt;/script&gt; 2.在页面添加配置 &lt;script type="text/javascript"&gt; var url = location.href.. 阅读详情

相关推荐

Windows10检查Cuda和cuDNN是否安装成功?

简介:根据上篇博客已经完成了CUDA和cuDNN的安装步骤,完成对其的安装。下面将来检查它们是否安装成功。 1 .首先win+R进入CMD,在命令行输入nvcc -V,如下图所示,则表示cuda已经安装成功。 2 .进入到cuda的安装路径,C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\extras\demo_suite,找到如下两个.exe文件: 首先执行:deviceQuery.exe,查看是否出现如下界面: 然后执行bandwid

jhsignal的博客 14万+

【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

【论文阅读】LIME:Low-light Image Enhancement via Illumination Map Estimation(笔记最全篇)

文章目录AbstractIntroductionContributionMethodSpeed-up Method(1):ALM子问题 T子问题 G子问题 Z和μ\muμSpeed-up Method(2):权重变量实验conclusion 今天要整理的一篇论文是使用传统的方法解决低照度的问题,Guo等人于2016年发表在IEEE上,论文《LIME:Low-light Image Enhance...

florrie 2万+

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

RC电路(一):微分|积分

的取值不同,会导致输出波形和输入波形之间的关系也不同,由此也会产生不同的应用。输出为0,随着电容充满电,运放-输入端得到的分压为正最大值,可等效成一个电压源,且电压不可突变,此时电流反向为最大值,开始充电时的容抗为0,电压不可突变则电压差为0,运放。开始充电时的容抗为0,电压不可突变则电压差为0,运放。输入端得到的分压也不可突变,随着电容放完电,于是。,因电容上电压不能突变(来不及充电,相当于短路,由上分析可得积分运算电路中,输出为运放的负最大峰值,随着电容充满电,输出为运放的正最大峰值,随着电容放完电,

既有随处可栖的江湖,也有追风逐梦的骁勇! 1万+

一个基于ZYNQ、AXI接口的PL端卷积加速器,可由PS端调用(附Github源码)

一个基于ZYNQ,AXI接口的PL端卷积加速器,可由PS端调用开发板型号:Zynq-7000 SoC XC7Z305 FPGA开发平台:Vivado 2019.1;Github源码链接:https://github.com/CY0807/ZYNQ_PL_CONV_ACC。

跳跳骑士的博客 1306

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

Kubernetes集群环境搭建与初始化

Kubernetes是Google开源的一个容器编排引擎,它支持自动化部署、大规模可伸缩、应用容器化管理。在生产环境中部署一个应用程序时,通常要部署该应用的多个实例以便对应用请求进行负载均衡。在Kubernetes中,我们可以创建多个容器,每个容器里面运行一个应用实例,然后通过内置的负载均衡策略,实现对这一组应用实例的管理、发现、访问,而这些细节都不需要运维人员去进行复杂的手工配置和处理。

m0_75254642的博客 1525

【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

金融领域必备:Python爬虫+AI量化分析,爬取股票数据并实现智能交易策略

本文通过Python爬虫+AI量化分析的组合,实现了从股票数据爬取到智能交易策略的全流程。免费获取全维度金融数据(行情、财务、舆情),替代付费终端;用LSTM模型捕捉股价的时间序列规律,提升预测精度;基于Backtrader实现策略回测与优化,保证策略的有效性。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1373

Linux内核vmlinuz文件:了解和编程

vmlinuz文件是Linux操作系统中的内核镜像文件。它包含了Linux内核的压缩版本,用于引导和启动操作系统。vmlinuz文件通常是一个zImage格式的压缩内核镜像,其中包含了操作系统所需的核心功能和驱动程序。vmlinuz文件的名称可能会因Linux发行版和版本而有所不同,但它们的作用是相同的。在某些发行版中,vmlinuz文件可能被重命名为vmlinux或bzImage。

PixelDyno的博客 1351

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

ECU诊断功能测试:从理论到实践

ECU诊断功能是指通过特定的诊断协议与ECU进行通信,以获取ECU的状态信息、故障码、数据流等,并执行特定的诊断操作。诊断功能的主要目的是帮助开发人员和维修人员快速定位和解决ECU中的问题,确保车辆的安全性和可靠性。UDS协议是ISO 14229标准的一部分,定义了一系列的诊断服务,如读取故障码、清除故障码、读取数据流等。UDS协议基于CAN(Controller Area Network)总线,具有较高的灵活性和扩展性。读取故障码是诊断功能中最基本的操作之一。通过读取故障码,可以快速定位ECU中的问题。

闲人编程的博客 1807

[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

VC中sqlite的使用

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

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

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

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

【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] 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、付费专栏及课程。

余额充值