查找--7.1 Sear for a Range-- 图解

    技术2022-07-11  74

    /********************************************************************************************************** 描述 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]. 分析 已经排好了序,用二分查找

    **********************************************************************************************************/

    class Solution{ public: vector<int> searchRange(int A[], int n, int target){ const int l = distance(A, lower_bound(A, A + n ,target)); const int u = distance(A, prev(upper_bound(A, A +n, target))); if (A[l] != target) return vector<int>{-1, -1}; else return vector<int>{l, u }; } };

    查找左右范围,返回一个区间

    参考资料:

    LeetCode题解

     

     

     

    Processed: 0.024, SQL: 9