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

由于O(logn)时间要求,显然用二分查找。

思路是先用二分查找找到其中一个target,找不到则返回默认的ret值[-1, -1]

找到之后从这个位置往两边递归进行二分查找进行范围的拓展。

具体来说,ret[0]不断向左扩展,ret[1]不断向右扩展。

  1. class Solution {
  2. public:
  3. vector<int> searchRange(int A[], int n, int target) {
  4. vector<int> ret(, -);
  5. int left;
  6. int right;
  7. int low = ;
  8. int high = n-;
  9. while((left = binarySearch(A, low, high, target)) != -)
  10. {
  11. ret[] = left;
  12. high = left-;
  13. }
  14. low = ;
  15. high = n-;
  16. while((right = binarySearch(A, low, high, target)) != -)
  17. {
  18. ret[] = right;
  19. low = right+;
  20. }
  21. return ret;
  22. }
  23. int binarySearch(int A[], int left, int right, int target)
  24. {
  25. while(left <= right)
  26. {
  27. int mid = left + (right-left) / ;
  28. if(A[mid] == target)
  29. return mid;
  30. else if(A[mid] < target)
  31. left = mid + ;
  32. else
  33. right = mid - ;
  34. }
  35. return -;
  36. }
  37. };

【LeetCode】34. Search for a Range的更多相关文章

  1. 【一天一道LeetCode】#34. Search for a Range

    一天一道LeetCode系列 (一)题目 Given a sorted array of integers, find the starting and ending position of a gi ...

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

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

  3. 【LeetCode】74. Search a 2D Matrix

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...

  4. LeetCode OJ 34. Search for a Range

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

  5. 【LeetCode题意分析&解答】34. Search for a Range

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

  6. 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...

  7. 【LeetCode】702. Search in a Sorted Array of Unknown Size 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 二分查找 日期 题目地址:https://lee ...

  8. 【LeetCode】34. Find First and Last Position of Element in Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

  9. 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...

随机推荐

  1. 四边形优化dp

    理解: http://blog.renren.com/share/263498909/1064362501 http://www.cnblogs.com/ronaflx/archive/2011/03 ...

  2. iOS:UIScrollView控件和UIPageControl控件的详解

    UIScrollView滚动视图控件和UIPageControl分页视图控件:    UIScrollView用于显示多于一个屏幕的内容,超出屏幕范围的内容可以通过滑动进行查看,当然UIPagecon ...

  3. C语言存储类型

    看c专家编程,有说存储类型一直不太清楚.看到一篇文章讲解c的存储类型,讲解了c语言中的各种变量的存储类型,而且是从进程.内存的角度讲解的,以前从没有这样理解过,觉得挺有用的,在这里转载过来. 首先要来 ...

  4. [leetcode]Flatten Binary Tree to Linked List @ Python

    原题地址:http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 题意: Given a binary tree, fl ...

  5. $.jsonp()的简单使用

    // jsonp 获取 json 数据: $.jsonp({ url: GLOBAL.baseUrl + '/company/mobi_getposter.action', callback: 'ca ...

  6. 深入理解this和call、bind、apply对this的影响及用法

    首先看一道网易的面试题: var a = { a:"haha", getA:function(){ console.log(this.a); } } var b = { a:&qu ...

  7. (算法)前K大的和

    题目: 1.有两个数组A和B,每个数组有k个数,从两个数组中各取一个数加起来可以组成k*k个和,求这些和中的前k大. 2.有N个数组,每个数组有k个数,从N个数组中各取一个数加起来可以组成k^N个和, ...

  8. 高并发分布式环境中获取全局唯一ID[分布式数据库全局唯一主键生成]

    需求说明 在过去单机系统中,生成唯一ID比较简单,可以使用MySQL的自增主键或者Oracle中的sequence, 在现在的大型高并发分布式系统中,以上策略就会有问题了,因为不同的数据库会部署到不同 ...

  9. Javascript高级程序设计-对象

    学习Javascript,最难的地方是什么? Object(对象)最难,初学者不容易掌握. Javascript是一种基于对象(object-based)的语言,它的语法中没有class(类). C# ...

  10. ZH奶酪:JavaScript中的JSON.stringify() and JSON.parse()

    JSON.stringify() JSON.stringify()可以将任意的JavaScript值序列化成JSON字符串. 语法 JSON.stringify(value[, replacer [, ...