"Leetcode-Interview algorithm classic-java Implementation" "081-search in rotated Sorted array II (search rotated sorted array)"

Source: Internet
Author: User

"081-search in rotated Sorted array II (search rotated sorted array)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" Original Question

Follow to "Search in rotated Sorted Array":
What if duplicates is allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given the target was in the array.

Main Topic

Subsequent to "Search values in rotated array", if the values in the array allow repeating write a program to determine whether a given value is in the array.

Thinking of solving problems

First find the lowest number of the subscript, and then according to the number to find in which part of the search.

Code Implementation

Algorithm implementation class

 Public  class solution {     Public Boolean Search(int[] Nums,intTarget) {if(Nums! =NULL&& nums.length >0) {//Find the lowest element corresponding subscript            intMinindex = Findminindex (nums);//entire array is globally ordered            if(Minindex = =0) {returnBinarySearch (Nums,0, Nums.length-1, target); }//There are two local ordered intervals, such as 4 5 6 7 8 9 0 1 2 3            Else{//Tian Good and the last element of the next ordered interval is equal, return the corresponding subscript                if(Nums[nums.length-1] = = target) {return true; }//target may be in the next ordered interval                Else if(Nums[nums.length-1] > Target) {returnBinarySearch (Nums, Minindex, Nums.length-1, target); }//target may be in the previous ordered interval                Else{returnBinarySearch (Nums,0, Minindex-1, target); }            }        }return false; }/** * Binary search * * @param nums array * @param start start position * End of @param end Location * @param target Search destination * @return true found, false not found */     Public Boolean BinarySearch(int[] Nums,intStartintEndintTarget) {intMid while(Start <= end) {mid = start + ((End-start) >>1);if(Nums[mid] = = target) {return true; }Else if(Nums[mid] > target) {end = mid-1; }Else{start = mid +1; }        }return false; } Public int Findminindex(int[] nums) {//Parameter check        if(Nums = =NULL|| Nums.length <1) {Throw NewIllegalArgumentException (); }intLo =0;inthi = Nums.length-1;intMID =0;//Can exclude the global order of the array         while(Nums[lo] >= Nums[hi]) {//If there are only two elements, return the latter one            if(Hi-lo = =1) {mid = Hi; Break; } mid = Lo + ((Hi-lo) >>1);if(Nums[mid] = = Nums[lo] && Nums[mid] = = Nums[hi]) {//can only use sequential search method, cannot adopt lo++,hi--way                //Because Lo may be the last of a previous ordered array                //Hi may also be the first of the latter an ordered array                returnSequencesearchminindex (Nums, lo, HI); }//If mid is in the previous ordered array            if(Nums[mid] >= Nums[lo])            {lo = mid; }//If mid is in the next ordered array            Else if(Nums[mid] <= Nums[hi])            {hi = mid; }        }returnMid }/** * The subscript of the smallest value in the sequential search array, nums is rotated by an ordered array by an axis * * @param nums Search Array * @param start position * @param End position * @return the lowest index */     Public int Sequencesearchminindex(int[] Nums,intStartintEnd) { for(inti = start; I < end; i++) {if(Nums[i] > nums[i +1]) {returni +1; }        }returnStart }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47270969"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java Implementation" "081-search in rotated Sorted array II (search rotated sorted array)"

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.