[Leetcode] search for a Range

Source: Internet
Author: User

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

 

Question: according to the time complexity required by the question, use the binary method to set the private two variables begin and end, and record the final range. In the recursive binary process, if the target is found, the table below the target in the array keeps narrowing the begin and expanding the end, so that the begin and end are the maximum range.

The main steps are as follows:

  1. If a [Mid] = target, the begin and end values are updated based on the mid value. Then, determine whether the rightmost element of the array on the left is still equal to the target element. If the elements are equal, continue to search for the left array. The array on the right also needs to be processed;
  2. If a [Mid] <target, Recursively search the array on the right;
  3. If a [Mid]> Target, Recursively search the array on the left;

The search process for the array [,] is as follows:

So the final returned range is [].

The Code is as follows:

 1 public class Solution { 2     private int begin; 3     private int end; 4     public void BinarySearch(int[] A,int target,int s,int e){ 5         if(s > e) 6             return; 7         int mid = s + (e - s)/2; 8         if(target == A[mid]){ 9             if(mid < begin)10                 begin = mid;11             if(mid > end)12                 end = mid;13             if(mid - 1>=0 && A[mid-1] == target)14                 BinarySearch(A, target, s, mid-1);15             if(mid + 1 < A.length && A[mid+1] == target)16                 BinarySearch(A, target, mid+1, e);17         }18         else{19             if(A[mid] > target)20                 BinarySearch(A, target, s, mid-1);21             else {22                 BinarySearch(A, target, mid+1, e);23             }24         }25     }26     public int[] searchRange(int[] A, int target) {27         begin = A.length;28         end = -1;29         BinarySearch(A, target, 0, A.length-1);30         31         int[] answer = new int[2];32         if(begin == A.length && end == -1){33             answer[0] = answer[1] = -1;34         }35         else{36             answer[0] = begin;37             answer[1] = end;38         }39         return answer;40     }41 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.