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. 实现Asp.net Mvc分布式Session Redis群集

    Redis群集实现Asp.net Mvc分布式Session Session的缺点 众所周知Asp.net Session默认存储在IIS中,IIS的重启会导致Session丢失. 如果你的网站使用了 ...

  2. asp.net使用post方式action到另一个页面,在另一个页面接受form表单的值!(报错,已解决!)

    原文:asp.net使用post方式action到另一个页面,在另一个页面接受form表单的值!(报错,已解决!) 我想用post的方式把一个页面表单的值,传到另一个页面.当我点击Default.as ...

  3. windows(64位)下用vagrant+virtualbox 管理虚拟机具体解释

    windows下安装(64位) vagrant 跟 vituriebox http://blog.smdcn.net/article/1308.html Host: 127.0.0.1 Port: 2 ...

  4. Install Typical IIS Workloads

    原文 Install Typical IIS Workloads Introduction The IIS 7.0 and above modular architecture is designed ...

  5. malloc实现原理

    记得早一段时间,看到一本书上写过delete的一个..今天突然找啦一下资料: malloc()是C语言中动态存储管理 的一组标准库函数之中的一个.其作用是在内存的动态存储区中分配一个长度为size的连 ...

  6. SQL_sql的简单查询

    ***********************************************声明*************************************************** ...

  7. TCP在三次握手协议和四波(图)

    设定TCP需要建立一个三次握手的能力,断开需要握手.整步骤,如看到下面的附图: 先来看看怎样建立连接的. 首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资 ...

  8. neu1458 方格取数 dp解法

    题意: 有N * N个格子,每一个格子里有正数或者0,从最左上角往最右下角走,仅仅能向下和向右,一共走两次(即从左上角走到右下角走两趟),把全部经过的格子的数加起来,求最大值SUM,且两次假设经过同一 ...

  9. 线程问题、异常处理、自定义URL

    线程问题.异常处理.自定义URL   本节又带了一些常用的,却很难理解的问题,本节从文件上传功能的实现引出了线程使用,介绍了线程饥饿的解决方法,异常处理方法,了解RouteTable自定义路径 . 系 ...

  10. ASP.Net MVC 数据处理

    ASP.Net MVC 数据处理   第三天我们将学习Asp.Net中数据处理功能,了解数据访问层,EF,以及EF中常用的代码实现方式,创建数据访问层和数据入口,处理Post数据,以及数据验证等功能. ...