Search for a range, 在一个可能有重复元素的有序序列里找到指定元素的起始和结束位置

问题描述:给定一个有序序列,找到指定元素的起始和结束位置。例如:1234555,5,起始4结束6

算法分析:其实就是一个二分查找的利用。但是特殊就在不是找到某个元素,而是找到下标。也就是在nums[mid]=target时,要分析mid的左右元素。

public int[] searchRange(int[] nums, int target)
    {
        if(nums == null || nums.length == 0)
        {
            return null;
        }
        int[] arr = {-1,-1};
        binarySearch(nums, 0, nums.length - 1, target, arr);
        return arr;
    }

    public void binarySearch(int[] nums, int left, int right, int target, int[] arr)
    {
        int mid = (left + right)/2;
        if(left > right)
        {
            return;
        }
        if(nums[left] == target && nums[right] == target)//特例
        {
            arr[0] = left;
            arr[1] = right;
            return;
        }
        if(nums[mid] == target)
        {
            int templ = mid, tempr = mid;
            while(templ>=left && nums[templ]==target)
            {
                templ --;
            }
            arr[0] = templ+1;
            while(tempr<=right && nums[tempr]==target)
            {
                tempr ++;
            }
            arr[1] = tempr-1;
        }
        else if(nums[mid] < target)
        {
            binarySearch(nums, mid + 1, right, target, arr);
        }
        else
        {
            binarySearch(nums, left, mid - 1, target, arr);
        }
    }
时间: 2024-10-13 09:45:27

Search for a range, 在一个可能有重复元素的有序序列里找到指定元素的起始和结束位置的相关文章

LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't

[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 搜索一个范围

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,

LeetCode 33. Search in Rotated Sorted Array(在旋转有序序列中搜索)

Suppose an array sorted in ascending order 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

Search for a Range问题

Search for a Range问题 leetcode java 二分查找 1. 问题描述 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

leetcode 二分查找 Search for a Range

Search for a Range Total Accepted: 21480 Total Submissions: 78454My Submissions 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

LeetCode: Search for a Range [033]

[题目] 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 [

Leetcode::Longest Common Prefix &amp;&amp; Search for a Range

一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an array of strings. 分析: 这道题目最重要的知道什么叫prefix前缀, 否则一不小心就做成了最长子序列.前缀就是两个序列的前多少位 都要是一样的,不能跳着对齐,这样就比较简单了,也可以求很多个序列的公共前缀. 1 class Solution { 2 public: 3 string

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