[Leetcode] Search in rotated Sorted Array

Source: Internet
Author: User

Search in rotated Sorted Array

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.

Problem Solving Ideas:

Test instructions to find the number of targets in the rotation array, and to find the minimum number http://www.kangry.net/blog/?type=article&article_id=111 is a sibling topic. Similar to the two-point search, analysis can be.

Array elements: xx xx ...       xx xx ... Xx

Array subscript: Start Middle End

As shown above,

1, if the middle equals the target value, returns the middle, if start equals the target value, then returns the start, if End equals the target value, returns the end.

2, if the middle is greater than the target value, and start is less than the target value, the start to middle is the order part, and the target value must be in the start to middle part (if present), so the end is assigned to the MIDDLE-1

3, if the middle is less than the target value, and end is greater than the target value, indicating that middle to end is the order part, and the target value is definitely in the middle to the end part (if present), so start is assigned the value of middle+1

4, if the middle is greater than the target value, and start is greater than the target value, this should be discussed. If start to middle is not a sequential part, indicates that target is between start and middle (if present), otherwise between middle and end

5, if the middle is less than the target value, and end is less than the target value, sub-case discussion, if the middle to end is not a sequential part, then the target between middle to end (if present), otherwise between start and middle.


Here's the code:

Class Solution {Public:int search (vector<int>& nums, int target) {int start=0;        int End=nums.size ()-1;        int middle;            while (start<=end) {middle= (start+end)/2;            if (nums[middle]==target) {return middle;            }else if (nums[start]==target) {return start;            }else if (nums[end]==target) {return end;                }else{if (nums[middle]>target&&nums[start]<target) {end=middle-1;                }else if (nums[middle]<target&&nums[end]>target) {start=middle+1;                        }else if (nums[middle]>target&&nums[start]>target) {if (Nums[middle]>nums[start]) {                    start=middle+1;                    }else{end=middle-1;             }}else if (nums[middle]<target&&nums[end]<target) {       if (Nums[middle]<nums[end]) {end=middle-1;                    }else{start=middle+1;    }}}} return-1; }};


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