Search in rotated sorted array of leetcode, which refers to the minimum number of the rotation array of Offer

Source: Internet
Author: User

Search in rotated sorted Array

Suppose a sorted array is rotated at some unknown to you beforehand.

(I. e .,0 1 2 4 5 6 7Might become4 5 6 7 0 1 2).

You are given a target value to search. If found in the array return its index, otherwise return-1.

You may assume no duplicate exists in the array.

Class solution {public: int search (int A [], int N, int target) {int left = 0, Right = n-1; while (left <= right) {int mid = left + (right-left)> 1); if (a [Mid] = target) return mid; if (A [Mid]> A [right]) // The first increment interval {if (a [left] <= target & target <A [Mid]) right = mid-1; else left = Mid + 1;} else // The second increment interval {if (a [Mid] <target & target <= A [right]) left = Mid + 1; else right = mid-1 ;}} return-1 ;}};

Search in rotated sorted array II

Follow up for "search in rotated sorted array ":
What if duplicates are allowed?

Wocould this affect the run-time complexity? How and why?

Write a function to determine if a given target is in the array.

Class solution {public: bool search (int A [], int N, int target) {int left = 0, Right = n-1; while (left <= right) {int mid = left + (right-left)> 1); if (a [Mid] = target) return true; if (A [Mid] = A [right]) // traverse the last section {for (; Right> = mid; -- right) {if (a [right] = target) return true;} else if (a [Mid]> A [right]) // The first increment interval {if (a [left] <= target & target <A [Mid]) Right = mid-1; else left = Mid + 1 ;} else // The second increment interval {if (a [Mid] <target & target <= A [right]) Left = Mid + 1; else right = mid-1 ;}} return false ;}};

Sword refers to offer: Moving the first several elements of an array to the end of an array, which is called the rotation of an array. Input a rotation of an incremental sorting array and output the smallest element of the rotating array. For example, if an array {3, 4, 5, 1, 2} is a rotation of {1, 2, 3, 4, 5}, the minimum value of this array is 1.

Train of Thought: here we provide two similar ideas, which are not the same as those on the offoffoffer. I think the offoffoffis hard to understand, but the efficiency of this algorithm is not as high as that in the book, the nine-degree space has timed out.

Int rotatearray (int * data, int N) {int left = 0, Right = n-1, minvalue = int_max; while (left <= right) {int mid = left + (right-left)> 1); If (data [Mid] <minvalue) minvalue = data [Mid]; if (data [Mid] = data [right]) // linear traversal {for (; Right> = mid; right --) {If (minvalue> data [right]) minvalue = data [right] ;}} else if (data [Mid]> data [right]) Left = Mid + 1; // The first increment interval else right = mid-1; // The second increment interval} return minvalue ;}



Search in rotated sorted array of leetcode, which refers to the minimum number of the rotation array of Offer

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.