LeetCode34. Search for a Range

来源:互联网 发布:多益网络加班严重吗 编辑:程序博客网 时间:2024/06/02 06:02

题目

Given an array of integers sorted in ascending order, 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].
题目原意是给定一个排序好的递增数组,然后从中找出需要的数在数组里的下标最小值和最大值,没有就返回[-1, -1]

题目分析

题目要求时间复杂度为O(logn),而且数组已经排序,那么很自然想到用二分查找,只不过不是查找到一个就结束了,而是从这个的左和右继续做二分查找,直到左和右都没有这个数。一开始我为了方便并不是这样做的,我是先用二分查找找到这个数,然后令下标最小值等于找到的这个下标,然后分别–和++,直到数组中这两个下标的数并不是要找的数

public:    vector<int> searchRange(vector<int>& nums, int target) {        int start = 0, end = nums.size() - 1;        if (nums.size() == 0) {            vector<int> result;            result.push_back(-1);            result.push_back(-1);            return result;        }        while(1) {            int mid = (end - start) / 2 + start;            if (mid == start&&nums[start] != target&&nums[end] != target) {                vector<int> result;                result.push_back(-1);                result.push_back(-1);                return result;            }            if (mid == start&&nums[start] != target&&nums[end] == target) {                vector<int> result;                result.push_back(end);                result.push_back(end);                return result;            }            if (nums[mid] == target) {                int begin = mid, finish = mid;                while (begin - 1 >= 0 && nums[begin-1] == target) {                    begin--;                }                while (finish + 1 < nums.size() && nums[finish + 1] == target) {                    finish++;                }                vector<int> result;                result.push_back(begin);                result.push_back(finish);                return result;            }            else if (nums[mid] < target) {                start = mid;                continue;            }            else if (nums[mid] > target) {                end = mid;                continue;            }        }     }};

但是当数组中有很多个要查找的数的时候时间复杂度就很大,优点是时间复杂度在只有比较少要查找的数时要比一直二分查找小,另附一直二分查找的

vector<int> searchRange(int A[], int n, int target) {    int i = 0, j = n - 1;    vector<int> ret(2, -1);    // Search for the left one    while (i < j)    {        int mid = (i + j) /2;        if (A[mid] < target) i = mid + 1;        else j = mid;    }    if (A[i]!=target) return ret;    else ret[0] = i;    // Search for the right one    j = n-1;  // We don't have to set i to 0 the second time.    while (i < j)    {        int mid = (i + j) /2 + 1;   // Make mid biased to the right        if (A[mid] > target) j = mid - 1;          else i = mid;               // So that this won't make the search range stuck.    }    ret[1] = j;    return ret; }

不过这里其实还可以用c++自带标准库里的equal_range方法来直接解决,这个方法能找出vector里含有该数的范围,返回的是两个迭代器,如果不存在这个数迭代器指向同一个地方,否则至少会相隔1

vector<int> searchRange(vector<int>& nums, int target) {    auto bounds = equal_range(nums.begin(), nums.end(), target);    if (bounds.first == bounds.second)        return {-1, -1};    return {bounds.first - nums.begin(), bounds.second - nums.begin() - 1};}
原创粉丝点击