[LeetCode] 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 must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

Example 1:

Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]

Example 2:

Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]

 

這道題讓我們在一個有序整數數組中尋找相同目標值的起始和結束位置,而且限定了時間復雜度為 O(logn),這是典型的二分查找法的時間復雜度,所以這里也需要用此方法,思路是首先對原數組使用二分查找法,找出其中一個目標值的位置,然后向兩邊搜索找出起始和結束的位置,代碼如下:

 

解法一:

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        int idx = search(nums, 0, nums.size() - 1, target);
        if (idx == -1) return {-1, -1};
        int left = idx, right = idx;
        while (left > 0 && nums[left - 1] == nums[idx]) --left;
        while (right < nums.size() - 1 && nums[right + 1] == nums[idx]) ++right;
        return {left, right};
    }
    int search(vector<int>& nums, int left, int right, int target) {
        if (left > right) return -1;
        int mid = left + (right - left) / 2;
        if (nums[mid] == target) return mid;
        if (nums[mid] < target) return search(nums, mid + 1, right, target);
        else return search(nums, left, mid - 1, target);
    }
};

 

可能有些人會覺得上面的算法不是嚴格意義上的 O(logn) 的算法,因為在最壞的情況下會變成 O(n),比如當數組里的數全是目標值的話,從中間向兩邊找邊界就會一直遍歷完整個數組,那么下面來看一種真正意義上的 O(logn) 的算法,使用兩次二分查找法,第一次找到左邊界,第二次調用找到右邊界即可,具體代碼如下:

 

解法二:

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        vector<int> res(2, -1);
        int left = 0, right = nums.size();
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) left = mid + 1;
            else right = mid;
        }
        if (right == nums.size() || nums[right] != target) return res;
        res[0] = right;
        right = nums.size();
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] <= target) left = mid + 1;
            else right = mid;
        }
        res[1] = right - 1;
        return res;
    }
};

 

其實我們也可以只使用一個二分查找的子函數,來同時查找出第一個和最后一個位置。如何只用查找第一個大於等於目標值的二分函數來查找整個范圍呢,這里用到了一個小 trick,首先來查找起始位置的 target,就是在數組中查找第一個大於等於 target 的位置,當返回的位置越界,或者該位置上的值不等於 target 時,表示數組中沒有 target,直接返回 {-1, -1} 即可。若查找到了 target 值,則再查找第一個大於等於 target+1 的位置,然后把返回的位置減1,就是 target 的最后一個位置,即便是返回的值越界了,減1后也不會越界,這樣就實現了使用一個二分查找函數來解題啦,參見代碼如下:

 

解法三:

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        int start = firstGreaterEqual(nums, target);
        if (start == nums.size() || nums[start] != target) return {-1, -1};
        return {start, firstGreaterEqual(nums, target + 1) - 1};
    }
    int firstGreaterEqual(vector<int>& nums, int target) {
        int left = 0, right = nums.size();
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) left = mid + 1;
            else right = mid;
        }
        return right;
    }
};

 

Github 同步地址:

https://github.com/grandyang/leetcode/issues/34

 

類似題目:

First Bad Version

 

參考資料:

https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/

https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/discuss/14699/Clean-iterative-solution-with-two-binary-searches-(with-explanation)

https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/discuss/14701/A-very-simple-Java-solution-with-only-one-binary-search-algorithm

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM