【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 example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].

由于O(logn)时间要求,显然用二分查找。

思路是先用二分查找找到其中一个target,找不到则返回[-1, -1]

找到之后从这个位置往两边递归进行二分查找进行范围的拓展。

部分参考了xiao.he.587的思路。

class Solution {
public:
    vector<int> searchRange(int A[], int n, int target) {
        vector<int> ret(2, -1);
        int ind = Helper(A, 0, n-1, target);
        if(ind != -1)
        {//find one target
            int left = ind;
            int right = ind;
            ret[0] = left;
            ret[1] = right;
            //recursively extend left towards 0
            while((left = Helper(A, 0, left-1, target)) != -1)
                ret[0] = left;
            //recursively extend right towards n-1
            while((right = Helper(A, right+1, n-1, target)) != -1)
                ret[1] = right;
        }
        return ret;
    }
    int Helper(int A[], int low, int high, int target)
    {//binary search
        while(low <= high)
        {
            int mid = low + (high-low)/2;
            if(A[mid] == target)
                return mid;
            else if(A[mid] > target)
                high = mid-1;
            else
                low = mid+1;
        }
        return -1;
    }
};

时间: 2024-10-10 22:56:41

【LeetCode】Search for a Range的相关文章

【LeetCode】Search Insert Position (2 solutions)

Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,

【Leetcode】Search a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

【LeetCode】- Search Insert Position(查找插入的位置)

[ 问题: ] Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. 翻译:给你一个排好序的数组和一个目标值,请找出目标值可以插入数组的位置. [ 分析: ]

【LeetCode】Search in Rotated Sorted Array——旋转有序数列找目标值

[题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no d

【LeetCode】Search in Rotated Sorted Array 解题报告

[题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no d

【LeetCode】Search in Rotated Sorted Array II 解题报告

[题目] Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. [解析] 相比Search in Rotated Sorted Array,在

【Leetcode】 Search in Rotated Sorted Array

题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array/ 题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in th

【leetcode】Search in Rotated Sorted Array II

Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 与I类似,只是在二分搜

【leetcode】Search in Rotated Sorted Array

Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise retu