LeetCode[Array]: Search for a Range

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].

我的解题思路是:首先用二分查找找到目标元素,然后在缩小后的搜索区域的前半部分利用二分查找找到起点,在后半部分利用二分查找找到终点。

我的C++代码实现如下:

vector<int> searchRange(int A[], int n, int target) {
    vector<int> range(2, -1);
    int start = 0, end = n - 1;
    while (start <= end) {
        int mid = (start + end) >> 1;
        if (A[mid] == target) {
            int start1 = start, end1 = mid;
            while (start1 <= end1) {
                int mid1 = (start1 + end1) >> 1;
                if (A[mid1] == target) end1 = mid1 - 1;
                else start1 = mid1 + 1;
            }
            range[0] = start1;

            start1 = mid, end1 = end;
            while (start1 <= end1) {
                int mid1 = (start1 + end1) >> 1;
                if (A[mid1] == target) start1 = mid1 + 1;
                else end1 = mid1 - 1;
            }
            range[1] = start1 - 1;

            break;
        }
        else if (A[mid] > target) end = mid - 1;
        else start = mid + 1;
    }

    return  range;
}

我在Discuss上看到一个非常巧妙的解法

You can choose two double numbers T-0.5 and T+0.5 for target T, and then binary search positions for the two double numbers in the integer array(suppose the position are a and b respectively), then the answer is [a, b-1]. For example, for input [5,7,7,8,8,10],
you can search position for number 7.5 and 8.5 using binary-search, and the result is 3 and 5, by which the answer [3,4] is easily obtained.

根据以上思路我做了如下实现:

class Solution {
public:
    vector<int> searchRange(int A[], int n, int target) {
        vector<int> range(2);
        int start = binarySearch(A, n, (double)target - 0.5);
        int end   = binarySearch(A, n, (double)target + 0.5);
        range[0] = (end > start ? start : -1);
        range[1] = (end > start ? end-1 : -1);
        return  range;
    }

private:
    int binarySearch(int A[], int n, double target) {
        int start = 0, end = n - 1;
        while (start <= end) {
            int mid = (start + end) >> 1;
            if ((double)A[mid] > target) end = mid - 1;
            else start = mid + 1;
        }

        return start;
    }
};
时间: 2024-12-28 09:27:41

LeetCode[Array]: Search for a Range的相关文章

leetcode题解:Search for a Range (已排序数组范围查找)

题目: 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,

[LeetCode] 034. Search for a Range (Medium) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Search for a Range (Medium) 链接: 题目:https://leetcode.com/problems/search-for-a-range/ 代码(github):https://github.com/illuz/leetcode 题意: 在有序数组中找到一个数的范围.(由于数

[LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. Your algorithm's runtime complexity

[LeetCode][JavaScript]Search for a Range

Search for a Range 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 ex

Leetcode 34. Search for a Range

34. Search for a Range Total Accepted: 91570 Total Submissions: 308037 Difficulty: Medium 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(l

【LeetCode】Search for a Range

Search for a Range 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 ex

LeetCode 034 Search for a Range

题目要求:Search for a Range 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]. F

leetCode 34.Search for a Range (搜索范围) 解题思路和方法

Search for a Range 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 ex

leetcode 【 Search for a Range 】python 实现

题目: 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,