"One Day together Leetcode" #33. Search in rotated Sorted Array

Source: Internet
Author: User

One Day together Leetcode

This series of articles has all been uploaded to my github address:
Https://github.com/Zeecoders/LeetCode
Welcome reprint, Reprint please indicate the source

(i) Title

Suppose a sorted array is rotated on some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You is given a target value to search. If found in the array is return its index, otherwise return-1.
Assume no duplicate exists in the array.

(ii) Problem solving

A well-ordered array that, after a certain amount of rotation, gets a new array, looking for a target number in this new array.
So, first we need to find the starting point of the original array in the rotated array and divide the array into two parts.
For example: 4,5,6,7,0,1,2 is divided into 4,5,6,7 and 0,1,2
Then, determine which part of the target number is,
Finally, the binary method is used to speed up the search!
See the code specifically:

Version not optimized
classSolution { Public:intSearch vector<int>& Nums,intTarget) {intLen = Nums.size ();inti =0;intj = len-1;intp =0; while(p+1<len&&nums[p]<nums[p+1]) p++;//Find the first break ascending number is the rotation center        if(nums[0]<=target) j=p;//Determine which part of the number is being searched        Else if(nums[len-1]>=target) i=p+1; while(I&LT;=J)//Two points search{intMid = (i+j)/2;if(Nums[mid] = = target)returnMidElse if(Nums[mid] >target) j= mid-1;Elsei = mid+1; }return-1;//Not found returns-1}};

After AC a look at the running time of 8ms, it seems that the code is still to be optimized, so in the search for the rotation center of the method to optimize, using the dichotomy of searching rotation center.

Optimized version
classSolution { Public:intSearch vector<int>& Nums,intTarget) {intLen = Nums.size ();inti =0;intj = Len-1;intp =0;BOOLIsfind =false;if(nums[0]>nums[len-1])//If a rotation is made{ while(I < J)//Two points search rotation center{if(i = = J-1) {Isfind =true; Break; }intMid = (i + j)/2;if(Nums[i] < Nums[mid]) i = mid;if(Nums[j] > Nums[mid]) j = mid; }        }if(Isfind)//found the rotation center{if(nums[0] <= target) {j = i; i =0; }if(Nums[len-1] >= target) {i = j; j = Len-1;} } while(i<=j) binary search target number {intMid = (i + j)/2;if(Nums[mid] = = target)returnMidElse if(Nums[mid] >target) j = mid-1;Elsei = mid+1; }return-1; }};

Optimized version run time 4ms, half faster!

"One Day together Leetcode" #33. Search in 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.