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(logn).

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

二. 题目分析

题目大意是,给定一个已排序的序列和一个目标数字target,在这个序列中寻找等于target元素的下标范围。由于序列已经排好序,直接用二分查找,分别求等于target的最靠左的元素下标left和最靠右的元素下标right即可。

三. 示例代码

#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        int n = nums.size();
        int left = searchRangeIndex(nums, target, 0, n - 1, true);
        int right = searchRangeIndex(nums, target, 0, n - 1, false);
        vector<int> result;
        result.push_back(left);
        result.push_back(right);
        return result;
    }

private:
    int searchRangeIndex(vector<int>& nums, int target, int low, int high, bool isLeft)
    {
        while (low <= high)
        {
            int midIndex = (low + high) >> 1;
            if (nums[midIndex] == target)
            {
                int temp = -1;
                if (isLeft)
                {
                    if (nums[midIndex] == nums[midIndex - 1] && low < midIndex)
                        temp = searchRangeIndex(nums, target, low, midIndex - 1, true);
                }
                else
                {
                    if (nums[midIndex] == nums[midIndex + 1] && high > midIndex)
                        temp = searchRangeIndex(nums, target, midIndex + 1, high, false);
                }
                return temp == -1 ? midIndex : temp; // temp == -1时表示只有中间一个值等于target
            }
            else if (nums[midIndex] > target)
                high = midIndex - 1;
            else
                low = midIndex + 1;
        }

        return -1; // 找不到target,输出-1

    }
};

四. 小结

注意题目要求O(logn)的时间复杂度,算法写的不好可能会超时。

时间: 2024-10-31 10:00:02

leetcode笔记:Search for a Range的相关文章

[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题解: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][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] 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 【 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,

LeetCode 34. Search for a Range (找到一个范围)

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 e