"Face question" looking for the smallest value in the rotated sorted array

Source: Internet
Author: User

Title Description

Suppose a rotated sorted array whose starting position is unknown (e.g. 0 1 2 4 5 6 7 May become 4 5 6 7 0 1 2). You need to find the smallest of these elements. http://www.lintcode.com/zh-cn/problem/find-minimum-in-rotated-sorted-array/

Thinking of solving problems

The basic idea uses two points to find, but first of all to determine whether the sorted array is directly ordered, if it is 0 1 2 3 4 5 6 7 Such an array, the minimum value is the first value; then the dichotomy is used to find

Code Implementation
classSolution { Public:/** * @param num:a rotated sorted array * @return: The minimum number in the array */    intFindmin ( vector<int>&num) {//Write your code here        intLen = Num.size ();intLow =0;intHigh = Len-1;if(Num[low] < Num[high])returnNum[low]; while(High-Low >1)        {intMid = (high + low)/2;if(Num[mid] > Num[low])             {low = Mid; }Else{high = mid; }        }returnNum[high]; }};
classSolution { Public:/** * @param num:a rotated sorted array * @return: The minimum number in the array */    intFindmin ( vector<int>&num) {//Write your code here        intLen = Num.size ();intLow =0;intHigh = Len-1;intMin_index = low; while(Num[low] >= Num[high]) {if(High-Low = =1) {Min_index = high; Break; }intMid = (high + low)/2;if(Num[low] < Num[mid]) low = mid;ElseHigh = mid; }returnNum[min_index]; }};

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Face question" looking for the smallest value in the 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.