LeetCode 34. 搜索范围(search for a range)
题目描述
给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。
你的算法时间复杂度必须是 O(log n) 级别。
如果数组中不存在目标值,返回 [-1, -1]。
示例 1:
输入: nums = [5,7,7,8,8,10], target = 8
输出: [3,4]
示例 2:
输入: nums = [5,7,7,8,8,10], target = 6
输出: [-1,-1]
解题思路
利用二分查找的思想,分别找到数组中target出现的首位置和末位置,其中找首位置的步骤如下:
- 若数组中点值小于target,继续在中点值之后的数组查找
- 若数组中点值大于或等于target,说明前面数组中还可能有target,继续在中点值之前的数组查找
- 最后当首位置大于末位置时,首位置即为数组中第一个大于或等于target的值
- 若首位置上的数与target相等,则返回该位置,否则返回-1
同理可得到找末位置的步骤。
代码
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
if(nums.empty())
return {-,-};
int first=findFirst(nums,target);
int last=findLast(nums,target);
return {first,last};
}
int findFirst(vector<int>& nums, int target){
int f=,l=nums.size()-;
while(f<=l){
int m=(f+l)/;
if(nums[m]<target)
f=m+;
else
l=m-;
}
if(f<nums.size()&&nums[f]==target)
return f;
return -;
}
int findLast(vector<int>& nums, int target){
int f=,l=nums.size()-;
while(f<=l){
int m=(f+l)/;
if(nums[m]<=target)
f=m+;
else
l=m-;
}
if(l>=&&nums[l]==target)
return l;
return -;
}
};
LeetCode 34. 搜索范围(search for a range)的更多相关文章
- 【LeetCode OJ 34】Search for a Range
题目链接:https://leetcode.com/problems/search-for-a-range/ 题目:Given a sorted array of integers, find the ...
- LeetCode(34)Search for a Range
题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...
- LeetCode OJ:Search for a Range(区间查找)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetCode 34.Search for a Range (搜索范围) 解题思路和方法
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)
原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...
- 【LeetCode】34. Search for a Range
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- leetcode 34 Search for a Range(二分法)
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
随机推荐
- php 技术点积累
PHP 反射之动态代理 php跨域的几种方式 给 PHP 开启 shmop 扩展实现共享内存 php十进制转二进制不用函数 php+nodeJs+thrift协议,实现zookeeper节点数据自动发 ...
- k8s部分名称解释
k8s部分名词解释 NameSpace:命名空间 Namespace是对一组资源和对象的抽象集合,比如可以用来将系统内部的对象划分为不同的项目组或用户组.常见的pods, services, repl ...
- 织梦DEDEcms5.7解决arclist标签调用副栏目文章
使用arclist标签调用文章的时候才发现,根本无法调用相关文章. 下面给出解决办法,希望帮到需要的人. 找到/include/taglib/arclist.lib.php文件然后打开.然后在大约30 ...
- Delphi 过程
- MobileNet系列
最近一段时间,重新研读了谷歌的mobilenet系列,对该系列有新的认识. 1.MobileNet V1 这篇论文是谷歌在2017年提出了,专注于移动端或者嵌入式设备中的轻量级CNN网络.该论文最大的 ...
- deep_learning_Function_tensorflow_reshape()
numpy.reshape(a, newshape, order='C')[source],参数`newshape`是啥意思? 根据Numpy文档(https://docs.scipy.org/doc ...
- 构建一个highcharts
示例:http://www.helloweba.com/demo/highcharts/line.html <!doctype html> <html lang="en&q ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(17)|装箱crates]
[易学易懂系列|rustlang语言|零基础|快速入门|(17)|装箱crates] 实用知识 装箱crates 我们今天来讲讲装箱技术crates. 什么是crates? 英语翻译是: 英 [kre ...
- oracle的监听控制
来自网络: listener control 监听控制 因为你在键入 lsnrctl 回车之后,就进入到监听控制界面. 在启动.关闭或者重启oracle监听器之前确保使用lsnrctl status命 ...
- LINUX笔记之二常用命令(文件处理命令)
一.概述 1. “.”开头的文件是隐藏文件,大小写敏感是因为用C语言编写 2. DOS中 cd..可回到父目录 在LINUX中要用cd ..(用空格) 3. 4.LINUX命令有两种:仅root可执行 ...