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

题目简述:给定一个有序的整型数组,找出给定的目标值的start和end下标。

算法的时间复杂度必须是O(log n)

如目标值没有发现,返回[-1,-1].

如给定一个数组[5,7,7,8,8,10],给定目标值8,

返回[3,4]。

思路:

  按照折半查找的方法查找到给定的目标值,得到相应的下标,在下标的两侧进行查找,找到相同的值.

int* searchRange(int* nums, int numsSize, int target, int* returnSize)
{
int *res=(int*)malloc(sizeof(int)*);
for(int i=;i<;i++)res[i]=-;
int low=;
int high=numsSize-;
int start=-,end=-;
if(low>high)return res;
*returnSize=;
while(low<=high)
{
int mid=(low+high)/;
if(nums[mid]>target)
{
high=mid-;
}
else if(nums[mid]<target)
{
low=mid+;
}
else{
start=mid;
end=mid;
while(start>low&&nums[start-]==nums[start])start--;
while(end<high&&nums[end+]==nums[end])end++;
res[]=start;
res[]=end;
return res;
}
}
return res;
}

Search for a Range的更多相关文章

  1. Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference

    最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...

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

  3. [OJ] Search for a Range

    LintCode 61. Search for a Range (Medium) LeetCode 34. Search for a Range (Medium) class Solution { p ...

  4. [LeetCode] 034. Search for a Range (Medium) (C++/Java)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  5. [Leetcode][Python]34: Search for a Range

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...

  6. leetCode 34.Search for a Range (搜索范围) 解题思路和方法

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

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

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

  8. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  9. 【LeetCode】34. Search for a Range

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

  10. LeetCode: Search for a Range 解题报告

    Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...

随机推荐

  1. TPS40305 ——开关电源芯片20160901

    TPS4030X芯片共有3款,区别在于开关频率不同. 学习笔记: 1.当开关频率越高,所使用的电感的容量越小,电路越稳定. 2.对于mos管源极的地,尽量和mos管漏极的电源输入的地接近,并且与最终的 ...

  2. JQuery基础总结上

    最近在慕课网学习JQuery基础课程,发现只是跟随网站的课程学习而不去自己总结扩展的话,很难达到真正学会理解的地步. 于是先在网站上草草过了一遍课程,然后借着今天的这个时间边记录边重新整理学习一下. ...

  3. VS2010默认属性文件配置

    问题: 在VS2010中,同一个解决方案下有多个项目,都需要使用某一个库. 如果项目比较多,或者编译链接环境属性变动频繁,分别对项目进行配置就很麻烦. 解决: 在VS的配置文件中统一配置属性: 我的配 ...

  4. linux tar命令的使用

    tar格式,会打包成一个文件,可以对多个目录,或者多个文件进行打包 tar命令只是打包,不会压缩,打包前后大小是一样的 tar命令 -c    //打包 -x    //解压 -f    //指定文件 ...

  5. 关于.NET 的邮件发送类

    .NET 类库中已经有现成的封好的类库了,只要引用System.Net.Mail命名空间即可实现发邮件的功能 以下是代码 public class SendMail { private string ...

  6. .NET网页打印以及使用打印需要注意的事项(可能会引起VS崩溃的现象、打印预览后关闭功能不管用)

    这两天进行给网页添加打印.打印预览.页面设置的功能.遇到了以下几个问题 [1]在网上查找了一些打印方法,一开始还可以用,后来不知道动到了哪里,点击vs中拆分或者切换到另一个设计和源代码显示方式,就会引 ...

  7. Android深度探索--HAL与驱动开发----第一章读书笔记

    1. Android的系统架构有四层,它的发展目前来说 是比较成熟的,流行于目前的市场.其架构包括四层(linux内核.C/C++代码库.Android SDK API.应用程序). 2. 驱动是直接 ...

  8. 使用Excel 2007绘制甘特图

    本文将教大家如何使用Excel 2007制作甘特图.Excel并未提供甘特图类型,但还是可以绘制甘特图的,方法就是通过对堆积条形图类型进行自定义,使之显示任务.任务工期和层次结构. 下面的过程可帮助创 ...

  9. Jqgrid学习API

    JQGrid是一个在jquery基础上做的一个表格控件,以ajax的方式和服务器端通信. JQGrid Demo 是一个在线的演示项目.在这里,可以知道jqgrid可以做什么事情. 下面是转自其他人b ...

  10. DNS配置详解

    DNS简介在Linux中,域名服务(DNS)是由柏克莱网间名域(Berkeley Internet Name Domain——BIND)软件实现的.BIND是一个客户/服务系统,它的客户方面称为转换程 ...