【LeetCode】【找元素】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]

思路一:一次Binary Search

先使用二分查找找到和taget相等的起始位置的元素,然后在这个位置向两边扩散找到值相同的范围。

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        vector<int> res(2,-1);
        if(nums.size() == 0) return res;
        int cau = dichotomy(nums,target);
        if(cau == -1) return res;
        else{
            int i = cau,j = cau;
            cout<<cau<<endl;
            while((i>=0 && nums[i] == target) || (j<=nums.size()-1 && nums[j] == target)){
                if(i>=0 && nums[i] == target){
                    res[0] = i;
                    cout<<i<<endl;
                    i--;
                }
                if(j<=nums.size()-1 && nums[j] == target){
                    res[1] = j;
                    cout<<j<<endl;
                    j++;
                }
            }
        }
        return res;
    }

    int dichotomy(vector<int>& nums, int target){
        int low = 0,int high = nums.size() - 1;
        while(low < high){
            int mid = (low + high + 1) / 2;
            if(nums[mid] == target) return mid;
            if(nums[mid] < target) low = mid + 1;
            else high = mid - 1;
        }
        return -1;
    }
};

思路二:两3次Binary Search

先根据上述的二分查找,找到起始位置的元素,然后从low开始继续使用一次二分查找找到target+1的位置,然后返回这个位置it - 1,从而找到起始和终止的范围。

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        vector<int> res(2,-1);
        if(nums.size() == 0) return res;
        int low = dichotomy(nums,target,0);   //找起始元素
        cout<<low<<endl;
        if(low == nums.size() || nums[low] != target)  //可能出现到数组结尾还是target比low处元素大,low=nums.size
            return res;
        else{
            res[0] = low;
            res[1] = dichotomy(nums,target+1,low) - 1;   //找结尾元素
        }
        return res;
    }

    int dichotomy(vector<int>& nums, int target, int low){
        int high = nums.size(); //核心
        while(low < high){
            int mid = (low + high ) / 2;
            if(nums[mid] < target) low = mid + 1;
            else high = mid;
        }
        return low;
    }
};

原文地址:https://www.cnblogs.com/ygh1229/p/9726966.html

时间: 2024-10-11 06:58:59

【LeetCode】【找元素】Find First and Last Position of Element in Sorted Array的相关文章

Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 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 v

[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Array

[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Array 本题难度: Medium/Medium Topic: Binary Search Description Given a sorted array of n integers, find the starting and ending position of a given target va

[leetcode][34] Find First and Last Position of Element in Sorted Array

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)

[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

刷题34. Find First and Last Position of Element in Sorted Array

一.题目说明 题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n).题目的难度是Medium! 二.我的解答 这个题目还是二分查找(折半查找),稍微变化一下.target==nums[mid]后,需要找前面.后面的值是否=target. 一次写出来,bug free,熟能生巧!怎一个爽字了得! #include<iostream> #include<vecto

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

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

[二分搜索] leetcode 34 Find First and Last Position of Element in Sorted Array

problem:https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/ 经典二分搜索题.要点是改变low或high的时候把当前数字mid也包含进来,因为它也可能是结果. class Solution { public: vector<int> searchRange(vector<int>& nums, int target) { vector<int

Find First and Last Position of Element in Sorted Array

问题:给定一个有序数组和一个目标值,输出目标值在数组中的起始位置和终止位置,如果目标值不在数组中,则输出[-1,-1] 示例: 输入:nums = [1,2,3,5,5,7] target = 5 输出:[3,4] 输入:nums = [1,5,8,9] target = 7 输出:[-1,-1] 解决思路:二分查找直到找到第一个目标值,再对目标值左右进行二分查找 Python代码: class Solution(object): def searchRange(self, nums, targ