Leetcode 6. Searching for search in rotated Sorted array after an ordered array rotation

Source: Internet
Author: User

Search in rotated Sorted Array
Difficulty: Hard

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.

(The next topic will study the existence of duplicate cases search in rotated Sorted Array II)

Problem Solving Ideas:
For array search problems, there are roughly three time complexity O(N),O(Lg  N),O(1)
Where linear complexity o ( n ) , that is, sequential search, and constant time o ( 1 ) Generally implemented by keyword index or hash table, and logarithmic complexity < Span class= "Mrow" id= "mathjax-span-848" > o ( l g &NBSP, n ) , often by binary lookup, binary search tree type implementation

For the condition in the subject is the deformation of an ordered array, so you can think of "quick sort Selection" – Two-point lookup
the difficulty is to find the dividing element and the recursive condition and its boundary between the left and right sides of the dividing element .

For dividing elements, it is easy to think of directly taking m = (l+r) / 2; the intermediate array boundary [L, R]
Observe the example:
0 1 2 4 5 6 7
4 5 6 7 0 1 2
5 1 3
3 1
NUMS[M] > Nums[l]: (L, M-1) monocytogenes
NUMS[M] <= Nums[l]: (m+1, R) monocytogenes
Note:
-When the array takes the boundary [L, R], M takes to the center of the right (as in 2 elements, M = 1), so at this time, should compare nums[m], num[l]
-When the array boundary [L, R] is taken, M is centered on the left (as in 2 elements, M = 0), so at this time, it should be compared nums[m], num[r];

Array boundary is [L,r] – < Span class= "Mrow" id= "mathjax-span-42" > o ( l g &NBSP, n )
classSolution { Public://nums array boundary is [L,r]    intSEARCHR ( vector<int>& Nums,intLintRintTarget) {if(R <= L)return-1;intm = (l+r)/2;if(Nums[m] = = target)returnMif(Nums[l] < nums[m]) {if(Target >= nums[l] && target < nums[m])returnSEARCHR (Nums, L, M, target);Else                returnSEARCHR (Nums, m+1, R, Target); }Else{if(Target > Nums[m] && target <= nums[r-1])returnSEARCHR (Nums, m+1, R, Target);Else                returnSEARCHR (Nums, L, M, target); }    }intSearch vector<int>& Nums,intTarget) {returnSEARCHR (Nums,0, Nums.size (), target); }};
Array bounds [L, r]– Note the bounds of the incoming array
classSolution { Public://nums array boundary is [L,r]    intSEARCHR ( vector<int>& Nums,intLintRintTarget) {if(R < L)return-1;intm = (l+r)/2;if(Nums[m] = = target)returnMif(Nums[r] > Nums[m]) {if(Target > Nums[m] && target <= nums[r])returnSEARCHR (Nums, m+1, R, Target);Else                returnSEARCHR (Nums, L, M-1, target); }Else{if(Target >= nums[l] && target < nums[m])returnSEARCHR (Nums, L, M-1, target);Else                returnSEARCHR (Nums, m+1, R, Target); }    }intSearch vector<int>& Nums,intTarget) {returnSEARCHR (Nums,0, Nums.size ()-1, target); }};
Sequential search O(n) – Not recommended
class  solution {public : int  search (vector  <int ;  & Nums, int  target" {int  i = 0 ; for  (; i < nums.size (); i++) {if  (nums[i] = = target) break;  } if  (nums.size () = = i) return -1 ;            else     return  i; }};

(The next topic will study the existence of duplicate cases search in rotated Sorted Array II)

Leetcode 6. Searching for search in rotated Sorted array after an ordered array rotation

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.