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 ...
随机推荐
- 刚接触SkyLine的一点小收获与感触
因为刚接触Skyline不到一个星期,也怕把学习到的忘记掉,所以写一点学习到的一些皮毛的东西,赶紧记录一下,怕回头忘记 1.网上关于web端的开发非常多,也有很多牛人分享自己的经验,所以学习起来也相对 ...
- 常见Http访问错误小结
4xx 客户端错误# 400 bad request 错误的请求 # 401 未携带身份信息 # 403 forbidden 权限不够 # 404 Not Found# 405 请求方式不允许 5xx ...
- jquery事件绑定方式总结(补充)
总结 : 1.简单事件绑定方式:事件名() 如:click() 2.高级事件绑定方式:bind(事件名,数据参数,function) 3.动态生成元素事件绑定方式:live(事件名,数据参数, ...
- 全局捕获异常(适用于SpringMvc,SpringBoot项目)
@ControllerAdvice 是controller的一个辅助类,最常用的就是作为全局异常处理的切面类.约定了几种可行的返回值,可以返回String字符串,也可以返回ModelAndView,也 ...
- MySQL配置文件my.cnf中文详解
#BEGIN CONFIG INFO #DESCR: 4GB RAM, 只使用InnoDB, ACID, 少量的连接, 队列负载大 #TYPE: SYSTEM #END CONFIG INFO # # ...
- Centos7.0 三种网络适配器
VMnet0:桥接模式 VMnet1:主机模式 VMnet8:NAT模式 VMware Network Adepter VMnet1:宿主Host用于与 主机模式 虚拟网络进行通信的虚拟网卡 VMwa ...
- 关于Java中线程取值并返回的方法
如何让一个线程不断跑起来,并且在取到值的时候能返回值而线程能继续跑呢? 我们都知道可以用Callable接口获得线程的返回值,或者触发事件监听来操作返回值,下面我将介绍另一种方法. public ab ...
- 数字转化为汉字,如5->五
//数字转化为汉字 如5-->五-(NSString*)translation:(NSString *)arebic{ NSString *str = arebic; NSArray ...
- JS---client系列
offset系列:获取元素的宽,高,left,top, offsetParent offsetWidth:元素的宽,有边框 offsetHeight:元素的高,有边框 offsetLeft:元素距 ...
- Nginx动静分离经典案例配置
随着Nginx高性能Web服务器大量被使用,目前Nginx最新稳定版为1.2.6,张宴兄在实际应用中大量使用Nginx,并分享Nginx高性能Web服务器知识,使得Nginx在国内也是飞速的发展.那今 ...