[LeetCode] Search for a Range [34]
题目
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]
.
解题思路
查找一个数出现的范围,给一个排好序的数组和一个数,找出这个数在数组中出现的范围。
这个题直接使用一次遍历就能够得到结果,这种时间复杂度为O(n)。可是对于有序数组我们一般能够使用二分查找能够得到更好的O(logn)的时间复杂度。我们能够使用二分查找找到这个数第一次出现的位置和这个数最后一次出现的位置,这样就能够得到它出现的区间。
代码实现
class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
vector<int> ret;
if(A==NULL || n<=0) return ret;
int first = getFirst(A, n, target);
int last = getLast(A, n, target);
ret.push_back(first);
ret.push_back(last);
return ret;
}
int getFirst(int A[], int n, int target){
int begin = 0, end = n-1;
int mid;
while(begin<=end){
int mid = (begin+end)/2;
if(A[mid] == target){
if(mid==0 || A[mid-1]<A[mid])
return mid;
else
end = mid-1;
}else if(A[mid] < target)
begin = mid+1;
else
end = mid-1;
}
return -1;
}
int getLast(int A[], int n, int target){
int begin = 0, end = n-1;
int mid;
while(begin<=end){
int mid = (begin+end)/2;
if(A[mid] == target){
if(mid==n-1 || A[mid+1]>A[mid])
return mid;
else
begin = mid+1;
}else if(A[mid] < target)
begin = mid+1;
else
end = mid-1;
}
return -1;
}
};
另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
)
[LeetCode] Search for a Range [34]的更多相关文章
- LeetCode: Search for a Range 解题报告
Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...
- [LeetCode] Search for a Range 搜索一个范围
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [LeetCode] Search for a Range(二分法)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode Search for a Range python
class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int ...
- [LeetCode] Search for a Range 二分搜索
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- Leetcode Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode:Search for a Range(数组,二分查找)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode -- Search for a Range (TODO)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- LeetCode Search for a Range (二分查找)
题意 Given a sorted array of integers, find the starting and ending position of a given target value. ...
随机推荐
- XML 关键字
SGML--Standard Generalized Marked Language 标准通用标记语言GML--Generalized Marked Language 通用标记语言XML--Extes ...
- linux安装Eclipse c++环境
yum install eclipse yum install eclipse-cdt
- u盘安装ubuntu10.04 server.txt
10.04 先将 ubuntu server 的 iso 放到优盘上,然后在提示无法找到光驱时,按 alt+f2 打开一个新的 console 窗口,将 iso mount 上,具体操作如下: ls ...
- error -27257: Pending web_reg_save_param/reg_find/create_html_param[_ex] request(s) detected and reset at the end of iteration number 1
检查点函数 web_reg_find("Search=body", "savecount=num", "Text=test1&quo ...
- 从svn下载项目后build path为灰色
今天从svn上下载项目后,想加入下面jar包.可是build path为灰色. 解决的方法是:1.在项目上右键properties---project facts 如图所看到的: 点击右側conver ...
- android用canvas绘制两种波纹效果
波形效果有几种不同的呈现形式,比如从中间向四周散开的波形,也就是熟知的水涟漪:还有上下波动的曲线,像五线谱等.英文中可以称作Wave或者Ripple,所以暂且叫它们WaveView.WaveLayo ...
- C语言深度解剖读书笔记(6.函数的核心)
对于本节的函数内容其实就没什么难点了,但是对于函数这节又涉及到了顺序点的问题,我觉得可以还是忽略吧. 本节知识点: 1.函数中的顺序点:f(k,k++); 这样的问题大多跟编译器有关,不要去刻意追求 ...
- 正确理解Python文件读写模式字w+、a+和r+
w+ 和 r+的差别不难理解.还有a+ +同一时候读写,就可以读又可写,边写边读.边读边写,不用flush,用seek 和 tell可測得. fp = open("a.txt", ...
- libevent简单分析
一看名字就知道是围绕eventloop转的. 那首先肯定是eventloop是个什么?一般都是IO事件,timer事件的管理器. 那首先看如何new出来一个eventloop: 1.因为libeven ...
- 【Demo 0002】Java基础-语句
本章学习要点: 1. 掌握Java关健语句使用方法; 2. 理解与语句相关的关键字用法; 一.Java 关键语句 Java语句以及关联关键字与C完全相 ...