zl程序教程

您现在的位置是:首页 >  其它

当前栏目

Search for a Range

for search range
2023-09-14 08:57:33 时间

在已经排好序的数组中查找,可用二分查找法效率更高,这里使用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