[Leetcode] search for a Range

Source: Internet
Author: User
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 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].

Analysis:

Sorted and time requiredO(LogN), That is, binary search is used. The preceding problem of binary search has been implemented: [leetcode] search insert position. The result is that its subscript is returned if it exists. If it does not exist, the subscript of the position it should have been inserted is returned.

The orthodox idea of this question should be to look for the target and continue to look for two boundaries on both sides. Binary classification is required for each search process.

However, I stole a lazy question and opened up O (n) space. I completely used the [leetcode] search insert position code.

Ideas:

Repackage the int [] array into double [], and search for target + 0.1 and target-0.1 in the double [] array. This is definitely not found, the return results of the Bipartite statements are the subscript begin and end that should be inserted. If begin = end, [-1,-1] is returned; otherwise, [begin, end-1] is returned.

The Code is as follows:

 1 public class Solution { 2     public int[] searchRange(int[] a, int target) { 3         if(a == null || a.length == 0) return new int[]{-1,-1}; 4         double[] aToDouble = new double[a.length]; 5         for(int i = 0; i < a.length; i++){ 6             aToDouble[i] = (double) a[i]; 7         } 8         int begin = binarySearch(aToDouble,(double)target - 0.1); 9         int end = binarySearch(aToDouble,(double)target + 0.1);10         if(begin == end){11             return new int[]{-1,-1};12         }else{13             return new int[]{begin,end - 1}; 14         }15     }16     private int binarySearch(double[] a,double target){17         int begin = 0, end = a.length - 1;18         while(begin <= end){19             int mid = (begin + end) >> 1;20             if(a[mid] > target){21                 end = mid - 1;22             }else{23                 begin = mid + 1;24             }25         }26         return begin;27     }28 }

 

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.