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 ...
随机推荐
- 阿里云服务器配置https
第一步 在阿里云控制台找到申请ssl证书的地址(我申请的是阿里云免费的证书,申请完要等申请通过,可能要等待一两天) 第二步下载ssl nginx版本的证书 第三步上传证书(包含.key, .pem这两 ...
- ASP.NET Core WebAPI帮助页--Swagger简单使用1.0
1.什么是Swagger? Swagger是一个规范且完整的框架,提供描述.生产.消费和可视化RESTful API,它是为了解决Web API生成有用文档和帮助页的问题. 2.为啥选用swagg ...
- win7-32位安装mysql-5.7.27
下载 https://dev.mysql.com/downloads/mysql/5.7.html#downloads 参考链接 https://blog.csdn.net/qq_41307443/a ...
- mongo启动报错问题处理
关键错误信息child process failed, exited with error number 100 这是服务器断电导致数据库意外关闭导致的问题,处理方法也比较简单 rm -rf /var ...
- linux系统查看某个用户错误登录次数
pam_tally2 --user user_name 查看user_name用户的错误登录次数 pam_tally2 --user user_name --reset 清空user_name用户的错 ...
- xml配置文件命名空间学习
xmlns(XML Namespaces的缩写)是一个属性,是XML(标准通用标记语言的子集)命名空间.作用是赋予命名空间一个唯一的名称. 编写Spring或者Maven或者其他需要用到XML文档的程 ...
- vim快速到行尾
快速到行尾A,或者End键(挨着Home键) 快速到第一行gg 快速到行首Home键,数字键的上面
- H5 2次重定向301
测试环境http ,生产https,导致请求重定向.加之H5自己的一次请求重定向,一共2次.
- 集合(三) HashMap
三.Map 先来讲一下Map,Map和Collection完全不是一个系列的,按理说讲完Collection的List,应该接着讲Collection的Set,但是因为Set中很多实现是基于Map来实 ...
- JSR303定义的校验类型
空检查 @Null 验证对象是否为null @NotNull 验证对象是否不为null, 无法查检长度为0的字符串 @NotBlank 检查约束字符串是不是Null还有被Trim的长 ...