博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Search for a Range
阅读量:4070 次
发布时间:2019-05-25

本文共 1009 字,大约阅读时间需要 3 分钟。

Given a sorted array of integers, 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].

For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].

class Solution {public:    vector
searchRange(int A[], int n, int target) { vector
re(2, -1); if(n == 0) return re; int low = 0; int high = n - 1; while(low <= high) { int mid = (low + high) >> 1; if(A[mid] == target) { low = mid; while(low - 1>= 0 && A[low - 1] == target) --low; while(mid + 1 <= high && A[mid + 1] == target) ++mid; re[0] = low; re[1] = mid; return re; } else if(A[mid] < target) low = mid + 1; else high = mid - 1; } return re; }};
找到这个值,则向左右延伸到其边界。

转载地址:http://relji.baihongyu.com/

你可能感兴趣的文章
企业云盘如何助力商业新发展
查看>>
医疗行业运用企业云盘可以带来什么样的提升
查看>>
能源化工要怎么管控核心数据
查看>>
媒体广告业如何运用云盘提升效率
查看>>
企业如何运用企业云盘进行数字化转型-实现新发展
查看>>
司法如何运用电子智能化加快现代化建设
查看>>
iSecret&nbsp;1.1&nbsp;正在审核中
查看>>
IOS开发的开源库
查看>>
IOS开发的开源库
查看>>
Jenkins - sonarqube 代码审查
查看>>
Jenkins + Docker + SpringCloud 微服务持续集成(一)
查看>>
Jenkins + Docker + SpringCloud 微服务持续集成 - 单机部署(二)
查看>>
Jenkins + Docker + SpringCloud 微服务持续集成 - 高可用集群部署(三)
查看>>
Golang struct 指针引用用法(声明入门篇)
查看>>
Linux 粘滞位 suid sgid
查看>>
C#控件集DotNetBar安装及破解
查看>>
Winform皮肤控件IrisSkin4.dll使用
查看>>
Winform多线程
查看>>
C# 托管与非托管
查看>>
Node.js中的事件驱动编程详解
查看>>