Search for a range Search for upper and lower bounds-Leetcode, range-leetcode

Source: Internet
Author: User

Search for a range Search for upper and lower bounds-Leetcode, range-leetcode
The original question is as follows:

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

The idea is as follows:

It is obvious that this is a question that examines the binary method. In my first thought, I used the binary method to find the target element, and then increased and decreased to the left and right sides. However, this is not the complexity of O (log n.

Later, I saw a very clever implementation in others' answers, using a variation in the binary method. The traditional binary method adopts the following structure:

 1     int left=0; 2     int right=length-1; 3     int middle=(left+right)/2; 4     while(left<right){ 5         if(middle>target){ 6             right=middle-1; 7         } 8         else if(middle>target){ 9             left=middle+1;10         }11         else{12         return middle;13         }14     }15     return left;

In this question, we do not need to find a specific element, but to find the upper and lower bounds of such a group of elements. Then we need to modify the binary method.

Instead of jumping out of the loop when an equal element is found, the boundary is pushed to the other end until the last of the equal element is found.

In this way, we can find the upper and lower bounds by running two different binary directions.

The Code is as follows:

1 public class Solution {2 public int [] searchRange (int [] nums, int target) {3 int left = 0, right = nums. length; // note that the right boundary is not nums. length-1. This is to facilitate the judgment of the 29th rows. 4 int mid = (left + right)/2; 5 while (left <right) {6 if (nums [mid]> = target) {7 right = mid; 8} 9 else {10 left = mid + 1; 11} 12 mid = (left + right)/2; 13} 14 int start = left; 15 left = start; 16 right = nums. length; 17 mid = (left + right)/2; 18 while (left <right) {19 if (nums [mid]> target) {20 right = mid; 21} 22 else {23 left = mid + 1; 24} 25 mid = (left + right)/2; 26} 27 int end = right; 28 return (start = end )? New int [] {-1,-1}: new int [] {start, end-1}; 29} 30}

There are alsoAn important trap:

Left + right may exceed the upper and lower bounds of int! The consequences are not easy to read!

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.