c_cpp [bs] [array]搜索范围。给定排序的整数数组,找到给定目标值的起始位置和结束位置。 O(log n)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp [bs] [array]搜索范围。给定排序的整数数组,找到给定目标值的起始位置和结束位置。 O(log n)相关的知识,希望对你有一定的参考价值。

// best version!
vector<int> searchRange(vector<int> &A, int target) {
        // write your code here
        if (A.empty()) return {-1, -1};
        int N = A.size();
        if (target < A[0] || target > A[N-1]) return {-1, -1}; // p4
        int left_edge = -1, right_edge = -1;
        
        int left = 0, right = N-1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (A[mid] < target) left = mid + 1;   // p3
            else right = mid - 1;
        }
        left_edge = A[left] == target ? left : -1;   // p1
        
        left = 0, right = N-1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (A[mid] > target) right = mid - 1;   // p2
            else left = mid + 1;
        }
        right_edge = A[right] == target ? right : -1;   // p0
        
        return {left_edge, right_edge};
}

// method 1, worse complexity O(N)

vector<int> search_range(vector<int> &A, int target) {
    vector<int> res;
    if(A.empty()) return res;
    int N = A.size();
    if(target < A[0] || target > A[N-1]) return res;
    
    int low = 0, high = N-1;
    while(low <= high) {
        int mid = low + (high - low)/2;
        if(A[mid] == target) {
            int left = mid, right = mid;
            while(left >= low && A[left] == A[mid]) left--;
            left++;
            while(right <= high && A[right] == A[mid]) right++;
            right--;
            res = {left, right};
            return res;
        } else if(A[mid] < target) low = mid+1;
        else high = mid-1;
    }
    return res;
}  

// method 2, worse complexity O(lgn)

int lower_bound(vector<int> &A, int target) {
    if(A.empty()) return -1;
    if(target < A[0] || target > A[A.size()-1]) return -1;
    int low = 0, high = A.size()-1;
    while(low <= high) {
        int mid = low + (high - low)/2;
        if(A[mid] >= target) high = mid-1;  // gist, note the '='
        else low = mid+1;
    }
    return low;
}

int upper_bound(vector<int> &A, int target) {
    if(A.empty()) return -1;
    if(target < A[0] || target > A[A.size()-1]) return -1;
    int low = 0, high = A.size()-1;
    while(low <= high) {
        int mid = low + (high - low)/2;
        if(A[mid] <= target) low = mid+1;   // gist, note the '='
        else high = mid-1;
    }
    return low-1;                           // gist, cannot forget minus 1
}

int main()
{
    vector<int> A = {1,2,3,3,3,4};
    vector<int> range = search_range(A, 3);
    cout << range[0] << ", " << range[1] << endl;
    cout << lower_bound(A, 3) << endl;
    cout << upper_bound(A, 3) << endl;
}

以上是关于c_cpp [bs] [array]搜索范围。给定排序的整数数组,找到给定目标值的起始位置和结束位置。 O(log n)的主要内容,如果未能解决你的问题,请参考以下文章

bs4 在给定标签的所有属性中搜索一个单词

c_cpp 围绕给定范围的阵列的三路分区

带有内部连接的 Laravel 范围

c_cpp 108.将Sorted Array转换为二进制搜索树

c_cpp 使用C ++库函数使用Suffix Array在文本中搜索模式

c_cpp 独特的二叉搜索树。给定n,生成存储值1 ... n的所有结构上唯一的BST(二叉搜索树)。