Two-point Search summary

Source: Internet
Author: User

In doing leetcode two points to find the type of topic write while conditions are always worried about the wrong, and indeed several times are to get wrong answer after the correct. In the search for a range, although was accepted, but see the online analysis of other students is not a special understanding, and finally determined to study the most classic binary search algorithm, to find out why the while conditions in different circumstances of the wording is not the same. Luckily, I soon found a great summary of the blog:
http://blog.csdn.net/daniel_ustc/article/details/17307937
After reading the implementation of their own, the implementation process of the while conditions under different circumstances why not the same understanding of the more profound.

The implementation of the three binary search methods to solve the following problems, respectively:
1) Find the subscript in the array where target appears, there is no return-1
2) Find the subscript position in the array for the first occurrence of the target, if there is no return-1
3) Find the last subscript position of the target in the array, if there is no return-1
PS: Arrays in ascending order, may have duplicate elements

1  PackageOrg.work.basic.binarysearch;2 3 ImportJunit.framework.Assert;4 Importorg.junit.Test;5 6  Public classBinarysearchcore {7 8 @Test9      Public voidTestbinarysearch () {Ten         int[] Nums = {0,1,1,1,2}; OneAssert.assertequals (2, BinarySearch (Nums, 1)); AAssert.assertequals (1, Binarysearchfirst (nums, 1)); -Assert.assertequals (3, Binarysearchlast (Nums, 1)); -         int[] Nums1 = {1}; theAssert.assertequals (0, BinarySearch (nums1, 1)); -Assert.assertequals (0, Binarysearchfirst (nums1, 1)); -Assert.assertequals (0, Binarysearchlast (nums1, 1)); -         int[] Nums2 = {}; +Assert.assertequals (0, BinarySearch (NUMS2, 1)); -Assert.assertequals (0, Binarysearchfirst (NUMS2, 1)); +Assert.assertequals (1, Binarysearchlast (NUMS2, 1)); A          at     } -     //find the subscript in nums[], if duplicate, return any one, if not found return-1 -      Public intBinarySearch (int[] Nums,inttarget) { -         if(Nums = =NULL|| Nums.length = = 0) -             return-1; -         intleft = 0, right = nums.length-1; in         intMID = 0; -          while(Left <=Right ) { toMid = left + ((right-left) >> 1); +             if(target = =Nums[mid]) -                 returnmid; the             Else if(Target <Nums[mid]) *right = Mid-1; $             Else Panax Notoginsengleft = mid + 1; -         } the         return-1; +     } A     //find the first subscript in nums[], if no return-1 is found the      Public intBinarysearchfirst (int[] Nums,inttarget) { +         if(Nums = =NULL|| Nums.length = = 0) -             return-1; $         intleft = 0, right = nums.length-1; $         intMID = 0; -          while(Left < right) {//there cannot be an equal sign here, or it may be executed in Right=mid, considering Case:[1], 1 -Mid = left + ((right-left) >> 1); the             if(Target >Nums[mid]) -left = mid + 1;Wuyi             Else theright =mid; -         } Wu         if(Nums[left] = =target) -             returnLeft ; About         return-1; $     } -     //find the last subscript in nums[], if no return-1 is found -      Public intBinarysearchlast (int[] Nums,inttarget) { -         if(Nums = =NULL|| Nums.length = = 0) A             return-1; +         intleft = 0, right = nums.length-1; the         intMID = 0; -          while(left + 1 < right) {//The loop only handles cases where the number of elements is greater than two, and two of the following elements may be executed in Left=mid (two elements when left==mid), considering case:[1,1],1 $Mid = left + ((right-left) >> 1); the             if(Target <Nums[mid]) theright = Mid-1; the             Else theleft =mid; -         } in         if(Nums[right] = =target) the             returnRight ; the         Else if(Nums[left] = =target) About             returnLeft ; the         return-1; the     } the}

Two-point Search summary

Related Article

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.