Search in rotated Sorted Array

Source: Internet
Author: User

Suppose an array sorted in ascending order are rotated at 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.

Your algorithm ' s runtime complexity must is in the order of O(log n).

Example 1:

Input:nums = [ 4,5,6,7,0,1,2] , target = 0output:4

Example 2:

Input:nums = [ 4,5,6,7,0,1,2] , target = 3Output:-1

This was my first code, it can ' t satisfy the last condition:

Class Solution {public:    int search (vector<int>& nums, int target) {        int len = Nums.size ();        if (len = = 0) return-1;        BOOL flag = FALSE;        if (Target < nums[0]) {for            (int i = len-1; i > 0;-i) {                if (nums[i] = = target) {                    flag = true;                    return i;}}        } else {for            (int i = 0; i < len; ++i) {                if (nums[i] = = target) {                    flag = true;                    return i;}}        }        if (!flag) {            return-1;}}    };

The right-to-use binary search:

Class Solution {public:    int search (vector<int>& nums, int target) {        int len = Nums.size ();        if (len = = 0) return-1;        int l = 0;         int r = len-1;        while (L <= r) {            int m = (L + r)/2;            if (nums[m] = = target) return m;            if (Nums[m] > Nums[r]) {                if (target < nums[m] && target >= nums[l]) {                    r = m-1;                } else {
   l = m + 1;                }            } else if (Nums[m] < nums[l]) {                if (target <= nums[r] && target > Nums[m]) {                    L = m + 1;                } el Se {                    r = m-1;                }            } else {                if (target < nums[m]) {                    r = m-1;                } else {                    L = m + 1;
   
    }}}        return-1;}    ;
   

Runtime: 8 ms, faster than 25.99% of C + + online submissions for Search in rotated Sorted Array.

But this is the more time before the previous.

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.