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的更多相关文章

  1. Leetcode::Longest Common Prefix && Search for a Range

    一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...

  2. LeetCode第[33]题(Java):Search in Rotated Sorted Array

    题目:在翻转有序中搜索 难度:Medium 题目内容: Suppose an array sorted in ascending order is rotated at some pivot unkn ...

  3. 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 ...

  4. 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 ...

  5. 【LeetCode OJ 34】Search for a Range

    题目链接:https://leetcode.com/problems/search-for-a-range/ 题目:Given a sorted array of integers, find the ...

  6. LeetCode(34)Search for a Range

    题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...

  7. LeetCode 34. 搜索范围(search for a range)

    题目描述 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. atitit.基于组件的事件为基础的编程模型--服务器端控件(1)---------服务器端控件和标签之间的关系

    atitit.基于组件的事件为基础的编程模型--服务器端控件(1)---------服务器端控件和标签之间的关系 1. server控件是要server了解了标签.种类型的server控件: 1 1. ...

  2. POJ 3979 分数减法【数学问题的探讨】

    将a/b和c/d简化一下就可以了 分数加减法 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12588   Accepted ...

  3. 联想G480安装CentOS电缆驱动器

    最近.联想G480 32本机安装现场CentOS 6.5. 发现.总是无法使用有线网络. 必须安装必要的驱动,搜集了资料,安装过程例如以下: 1. 必备的软件 安装前,须要下列的软件依赖包. sudo ...

  4. Rational Rose 2007使用小结

    1.Rose怎样隐藏类的属性和操作? 右击类,选Options->Suppress Attributes/Suppress Operations 2.Rose怎样表示类的约束? 在工具箱中选AB ...

  5. WinHEC(Windows硬件project产业创新峰会)将2015回归

    WinHEC这是Windows Hardware Engineering Cumminity,中国呼吁Windows硬件project产业创新峰会.将2015在早期的回报,2015年3月18日至19日 ...

  6. Swift: 打造滑动解锁文字动画

    原文:Swift: 打造滑动解锁文字动画 最近木事,找出来玩了玩facebook的paper.到处都是那个"slide to unlock your phone"的效果啊.忽闪忽闪 ...

  7. 关于接收POST请求 $GLOBALS['HTTP_RAW_POST_DATA']

    总是产生变量包含有原始的 POST 数据.否则,此变量仅在碰到未识别 MIME 类型的数据时产生.不过,访问原始 POST 数据的更好方法是 php://input.$HTTP_RAW_POST_DA ...

  8. 【甘道夫】官方网站MapReduce代码注释具体实例

    引言 1.本文不描写叙述MapReduce入门知识,这类知识网上非常多.请自行查阅 2.本文的实例代码来自官网 http://hadoop.apache.org/docs/current/hadoop ...

  9. C面试题

    1.sizeof()和strlen()使用? 答案: 1.从功能定义,strlen功能,要查找字符串的长度,sizeof功能是用来寻找指定的变量或变量类型的存储器占用 尺寸: 2.sizeof是运算符 ...

  10. Matrix (二维树状数组)

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...