leetcode第33题--Search for a Range
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].
给的那个一个有序数组和一个目标target,找到这个目标在有序数组里的开始和结束的下标。如果不存在就返回 -1 -1。注,如果数组中目标值只有一个,那就返回两个相同的下标。
用binary search找到target 然后再在左边binary search找到左边(包括mid)第一个目标,记录下标。再在右边binary 找到最后一个目标。记录下标。返回。
代码如下:
class Solution {
public:
vector<int> searchRange(int A[], int n, int target)
{
vector<int> wr, ans;
wr.push_back(-1); // 要学会可以用 vector<int> wr(2,-1);直接将其初始化为想要的
wr.push_back(-1);
if(n == 0)
return wr;
int l = 0, r = n -1, mid = 0;
while(l <= r)
{
mid = (l+r)/2;
if (A[mid] < target) // 如果mid小于target那么可以把左边一块去掉
{
l = mid + 1;
continue;
}
if (A[mid] > target) // 如果mid大于target那么可以把右边一块去掉
{
r = mid -1;
continue;
}
if (A[mid] == target)// mid 找到了刚好,那就肯定在这里面要有return,如果一直没有找到,跳出while后就返回wr
{
// find start from l to mid,binary search
int start = l, l_r = mid; // l_r指mid左边的最右
while(l <= l_r)
{
int tmp_mid = (l + l_r)/2;
if(A[tmp_mid] != target)
{l = tmp_mid + 1;}
if(A[tmp_mid] == target)
{l_r = tmp_mid - 1; start = tmp_mid;}
}
ans.push_back(start);
// find end from mid to r,binary search
int r_l = mid, End = mid; // r_l指mid右边的最左
while(r_l <= r)
{
int tmp_mid = (r_l + r)/2;
if(A[tmp_mid] != target)
{r = tmp_mid - 1;}
if(A[tmp_mid] == target)
{r_l = tmp_mid + 1; End = tmp_mid;}
}
ans.push_back(End);
return ans;
}
}
return wr;
}
};
leetcode第33题--Search for a Range的更多相关文章
- Leetcode::Longest Common Prefix && Search for a Range
一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...
- LeetCode第[33]题(Java):Search in Rotated Sorted Array
题目:在翻转有序中搜索 难度:Medium 题目内容: Suppose an array sorted in ascending order is rotated at some pivot unkn ...
- leetcode第32题--Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- LeetCode(33)Search in Rotated Sorted Array
题目 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 m ...
- 【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 34. 搜索范围(search for a range)
题目描述 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值 ...
- LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku
1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
随机推荐
- PHP操作数据库PDO
PHP操作数据库 载入数据库驱动 訪问phpinfo.php能够查看是否已经载入数据库驱动,例如以下显示还没有载入mySql数据库驱动. 在c盘找到php.ini配置文件开启载入mySql驱动,例如以 ...
- php用空格代替标点符号
php作为常规赛的符号替换为空格 <? php $character = "!@#$%^&*于'纸'纸'文().,<>|[]'\":;}{-_+=? /a ...
- 使用Advanced Installer 自动部署 Arcgis Engine Runtime 10.0
原文:使用Advanced Installer 自动部署 Arcgis Engine Runtime 10.0 目前采用Arcgis9.2 + c#(vs2008)作为程序开发平台,是一个不错的搭配. ...
- Cocos2d-X中的坐标系
在Cocos2d-x中坐标能够分成四种: 1.GL坐标体系:GL坐标体系左下角为坐标原点,X轴向右,Y轴向上 2.UI坐标体系:UI坐标体系左上角为坐标原点,X轴向右,Y轴向上. .世界坐标体系:是窗 ...
- STM32电源管理
(1)3时钟模式 ①睡眠模式②停止模式③待机模式 1.睡眠模式:Cortex-M3内核(理解为CPU)停止工作,CPU供电1.8V有着,周边任何执行.执行 2.停机模式:全部时钟都停止,CPU电 ...
- jquery.validate1.13
jquery.validate新的写法(jquery.validate1.13.js) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
- strip 使用命令
使用 通过消除使用调试器的粘合剂和符号信息,减少扩展公共对象文件格式(XCOFF)对象文件大小. 语法 strip [ -V ] [ -r [ -l ] | -x [ -l ] | -t | -H | ...
- hdu4324 Triangle LOVE (拓扑排序)
这是一道最简单的拓扑排序题,好久没看这个算法了! 有点生疏了! 后附上百度的资料; #include<stdio.h> #include<string.h> int in[50 ...
- measureChildren作品
无论是在改写View依然是ViewGroup什么时候.特别ViewGrop什么时候,通常是不可避免的重写onMeasure方法,我们一定会调用setMeasuredDimension()将測量好的宽高 ...
- Win8.1 Update如何禁用OneDrive同步服务
原文 Win8.1 Update如何禁用OneDrive同步服务 上周,IT之家为爱好者分享Win8.1 Update如何提高OneDrive上传速度教程.但是,由于国内特殊网络环境,导致微软OneD ...