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]

我的解:

Runtime: 12 ms, faster than 32.21% of C++ online submissions for Find First and Last Position of Element in Sorted Array.

Memory Usage: 10.2 MB, less than 98.90% of C++ online submissions for Find First and Last Position of Element in Sorted Array.

class Solution {
public:
    // 递归进行,二分查找
    void binSearch(vector<int>& nums, int target, int begin, int end, int& index1, int& index2)
    {
        while (begin <= end)
        {
            int mid = begin + (end - begin) / 2;
            if (nums[mid] == target)
            {
                if (mid < index1)index1 = mid;
                if (mid > index2)index2 = mid;
                if (begin == end) return ;
                if (mid > 0 && nums[mid - 1] == target) binSearch(nums, target, begin, mid - 1, index1, index2);
                if (mid < end && nums[mid + 1] == target) binSearch(nums, target, mid + 1, end, index1, index2);
                return;
            }
            if (nums[mid] < target)
            {
                begin = mid + 1;
            }
            if (nums[mid] > target)
            {
                end = mid - 1;
            }
        }
    }
    vector<int> searchRange(vector<int>& nums, int target) {
        vector<int> res{ -1,-1 };
        if (nums.size() < 1)return res;
        int b = 0;
        int e = nums.size() - 1;
        int index1 = e + 1;
        int index2 = b - 1;
        binSearch(nums, target, b, e, index1, index2);
        if (index1 <= index2)
        {
            res[0] = index1;
            res[1] = index2;
        }
        return res;
    }
};

优秀解1:

Runtime: 8 ms, faster than 85.03% of C++ online submissions for Find First and Last Position of Element in Sorted Array.

Memory Usage: 10.1 MB, less than 100.00% of C++ online submissions for Find First and Last Position of Element in Sorted Array.

class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
    int idx1 = lower_bound(nums, target);
    int idx2 = lower_bound(nums, target+1)-1;
    if (idx1 < nums.size() && nums[idx1] == target)
        return {idx1, idx2};
    else
        return {-1, -1};
}
// 利用二分查找的思想
int lower_bound(vector<int>& nums, int target) {
    int l = 0, r = nums.size()-1;
    while (l <= r) {
        int mid = (r-l)/2+l;
        if (nums[mid] < target)
            l = mid+1;
        else
            r = mid-1;
    }
    return l;
}
};

原文地址:https://www.cnblogs.com/qiang-wei/p/11801396.html

时间: 2024-08-07 20:40:13

34- Find First and Last Position of Element in Sorted Array的相关文章

[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 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

刷题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

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

[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】【找元素】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, -

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