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