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

Hide Tags

Array Binary Search

 

    这道是变形的二分搜索,写的有点冗余,没有很好的合并在一起。
  方法是先二分搜索到 其中一个target, 然后分别二分搜索边界。
  1. #include <vector>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class Solution {
  6. public:
  7. vector<int> searchRange(int A[], int n, int target) {
  8. vector<int > ret(,-);
  9. if(n<) return ret;
  10. int mid = helpFun(A,,n-,target);
  11. if(mid !=-){
  12. ret[]=helpLeft(A,,mid,target);
  13. ret[]=helpRight(A,mid,n-,target);
  14. }
  15. return ret;
  16. }
  17.  
  18. int helpLeft(int *a,int lft, int rgt, int & tar)
  19. {
  20. if(a[lft]==tar) return lft;
  21. if(lft+==rgt) return rgt;
  22. int mid = (lft+rgt)/;
  23. if(a[mid]==tar) return helpLeft(a,lft,mid,tar);
  24. else return helpLeft(a,mid,rgt,tar);
  25. }
  26.  
  27. int helpRight(int *a,int lft, int rgt, int & tar)
  28. {
  29. if(a[rgt]==tar) return rgt;
  30. if(lft+==rgt) return lft;
  31. int mid = (lft+rgt)/;
  32. if(a[mid]==tar) return helpRight(a,mid,rgt,tar);
  33. else return helpRight(a,lft,mid,tar);
  34. }
  35.  
  36. int helpFun(int *a,int lft, int rgt, int & tar)
  37. {
  38. if(lft>rgt) return -;
  39. if(lft == rgt){
  40. if(tar==a[lft]) return lft;
  41. return -;
  42. }
  43. if(lft + == rgt){
  44. if(a[lft]==tar) return lft;
  45. if(a[rgt]==tar) return rgt;
  46. return -;
  47. }
  48. int mid = (lft+rgt)/;
  49. if(a[mid]==tar) return mid;
  50. if(a[mid]<tar) return helpFun(a,mid+,rgt,tar);
  51. else return helpFun(a,lft,mid-,tar);
  52. }
  53. };
  54.  
  55. int main()
  56. {
  57. int A[]={, , , , , };
  58. Solution sol;
  59. vector<int > ret = sol.searchRange(A,sizeof(A)/sizeof(int),);
  60. cout<<ret[]<<ret[]<<endl;
  61. return ;
  62. }

附件是一份集成的比较好的,思路是一样:

  1. vector<int> searchRange(int a[], int n, int target) {
  2. vector<int> result(,-);
  3. int left = INT_MAX;
  4. int right = INT_MIN;
  5. binSearch(a, , n-, n, target, left, right);
  6. if (left != INT_MAX && right != INT_MIN) {
  7. result[] = left;
  8. result[] = right;
  9. }
  10. return result;
  11. }
  12. void binSearch(int a[], int l ,int h, int n, int target, int& left, int& right) {
  13. if (l > h) return;
  14.  
  15. int m = (l+h)/;
  16. if (a[m] > target) {
  17. binSearch(a,l,m-,n,target, left, right);
  18. } else if (a[m] < target) {
  19. binSearch(a,m+,h,n,target, left, right);
  20. } else {
  21. if (m < left) {
  22. left = m;
  23. if (m > && a[m-] == target) {
  24. binSearch(a,l,m-,n,target,left,m);
  25. }
  26. }
  27. if (m > right) {
  28. right = m;
  29. if (m < n- && a[m+] == target) {
  30. binSearch(a,m+,h,n,target,m,right);
  31. }
  32. }
  33. }
  34. }
 

[LeetCode] Search for a Range 二分搜索的更多相关文章

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

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

  2. [LeetCode] Search for a Range 搜索一个范围

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

  3. [LeetCode] Search for a Range(二分法)

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

  4. leetcode -- Search for a Range (TODO)

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

  5. leetcode Search for a Range python

    class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int ...

  6. [LeetCode] Search a 2D Matrix 二分搜索

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  7. Leetcode Search for a Range

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

  8. leetcode:Search for a Range(数组,二分查找)

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

  9. [LeetCode] Search for a Range [34]

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

随机推荐

  1. P1219 N皇后

    P1219 N皇后 题目描述 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子. 上面的布局可以用序 ...

  2. Android面试收集录16 Android动画总结

    一.Android 动画分类 总的来说,Android动画可以分为两类,最初的传统动画和Android3.0 之后出现的属性动画: 传统动画又包括 帧动画(Frame Animation)和补间动画( ...

  3. 局域网&广域网&Internet&计算机通信过程

    1.局域网 覆盖范围小(100m以内),自己花钱购买,自己单位来维护,带宽固定的(10M,100M,1000M) 2.Internet ISP,有自己的机房,对网民提供访问Internet连接 3.广 ...

  4. 直接插入排序&希尔排序

    1.直接插入排序 时间复杂度O(n2) 工作原理: 通过构建有序序列,对于未排序数据,在已排序的序列中,从后向前扫描,找到相应的位置并插入. 插入排序在实现上,在从后向前扫描的过程中,需要反复把已排序 ...

  5. svn Previous operation has not finished; run 'cleanup' if it was interrupted

    svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted Usually, a ...

  6. 安装macports

    Mac下面除了用dmg.pkg来安装软件外,比较方便的还有用MacPorts来帮助你安装其他应用程序,跟BSD中的ports道理一样.MacPorts就像apt-get.yum一样,可以快速安装些软件 ...

  7. android 文件保存

    将数据保存在外部存储器上 /* Checks if external storage is available for read and write */ public boolean isExter ...

  8. 阿里云ECS Ubuntu安装PHP+Mysql+Apache+Nginx+Redis+Discuz

    http://www.linuxdiyf.com/linux/13662.html http://blog.csdn.net/wangnan537/article/details/47868659 h ...

  9. [译]9-spring bean的生命周期

    spring中bean的生命周期比较容易理解.bean在实例化之后有时需要调用某个初始化方法进行一些初始化的工作.同样的 ,当bean在销毁之前有时需要做一些资源回收的工作. 尽管bean在实例化和销 ...

  10. JavaScript里面的正则以及eval

    1.eval JavaScript中的eval是Python中eval和exec的合集,既可以编译代码也可以获取返回值. eval() EvalError   执行字符串中的JavaScript代码 ...