Search for a Range

在已经排好序的数组中查找,可用二分查找法效率更高,这里使用STL更方便。

vector<int> searchRange(int A[], int n, int target)
      {
          int l = distance(A, lower_bound(A, A + n, target));
          int r = distance(A, prev(upper_bound(A, A + n, target)));

          if (A[l] != target)
              return{ -1, -1 };
          else
              return{ l, r };
      }
View Code
原文地址:https://www.cnblogs.com/573177885qq/p/5675326.html