"Leetcode" Find Minimum in rotated Sorted array finding the smallest number in a rotating array

Source: Internet
Author: User

ADD Date 2014-10-15

Find Minimum 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 ).

Find the minimum element.

Assume no duplicate exists in the array.

This is very simple, because there is no repeating number, the array is essentially ordered, and the method of finding with similar binary complexity O (log n). Code is not a few lines, look directly at code, remember to consider the entire array is not rotated situation.

1 classSolution {2  Public:3     intFindmin (vector<int> &num,intPreintPost) {4         if(Pre = =Post)5             returnNum[pre];6         if(Num[pre] <Num[post])7             returnNum[pre];8         intMid = (pre+post)/2;9         if(Num[mid] >=Num[pre])Ten             returnFindmin (num, mid+1, post); One         if(Num[mid] <Num[post]) A             returnfindmin (NUM, pre, mid); -     } -     intFindmin (vector<int> &num) { the         intLen =num.size (); -         returnFindmin (NUM,0, len-1); -     } -};

Continue to brush leetcode tomorrow.

"Leetcode" Find Minimum in rotated Sorted array finding the smallest number in a rotating 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.