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. js控制select下拉列表数据绑定

    JS代码部分:  <script type="text/javascript"> $(document).ready(function () { $("sel ...

  2. CheckBoxList控件获取多选择,需要遍历

    CheckBoxList控件获取多选择,需要遍历,环境:vs2008 在页面上添加CheckBoxList控件,输入项值 a,b,c,d.然后添加按钮 Button2确定,如何获取CheckBoxLi ...

  3. html canvas 弹球(模仿)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Objective-C Memory Management

    Objective-C Memory Management Using Reference Counting 每一个从NSObject派生的对象都继承了对应的内存管理的行为.这些类的内部存在一个称为r ...

  5. 【python】实用函数啥的

    1.测试运行时间/效率 t = time() print time() -t 2.C:\python344\Scripts   easy_install.exe ,提供包的名字,可以自动下载+装包 3 ...

  6. 部署Maven核心程序

    1.安装Maven核心程序 1.1 检查JAVA_HOME环境变量 2. 解压Maven核心程序压缩包,放在一个非中文无空格的路径下 3. 配置Maven相关的环境变量 3.1 MAVEN_HOME ...

  7. php判断手机还是pc

    <?php function isMobile(){ $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AG ...

  8. block的常见用法

    一.声明和定义 1.声明 声明方式:返回值(^block)(参数).声明时,参数变量名可以省略:使用时,参数变量名不能省略,不然会无法调用传入的参数 void(^block)(); void(^blo ...

  9. 关于XML文档

    很多人觉得XML很简单,但我想说其实一点也不简单,特别是在当今被各种web文档充斥的世界里有必要对XML进行一定程度的研究.下面的几篇博客将对XML技术进行简短的说明.

  10. PAT 1072. Gas Station (30)

    A gas station has to be built at such a location that the minimum distance between the station and a ...