[LeetCode] Find Minimum in Rotated Sorted Array

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/

Suppose a sorted array is 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).

Find the minimum element.

You may assume no duplicate exists in the array.

 

Hide Tags
 Array Binary Search
 
 
分析:
第一:low < high && A[low] > A[high 保证 转折点在 low和high之间
第二: if(low + 1 == high)     return A[high]; 只有两个元素时,返回A[high],因为A[low] > A[high]
第三:注意下面code中何时包含mid,何时不包含mdi
               if(A[low] < A[mid])//pivot is in bottom half, mid can't be lowest
                {   
                    low = mid + 1;
                }   
                else //pivot is in first half, mid may be lowest
                {   
                    high = mid;
                }               

第四:如果没进while循环,说明数组是有序的,没有经过rotate,所以返回A[low] 即可。

 
 
class Solution {
    public:
        int findMin(vector<int>& A)
        {
            int low = 0;
            int high = A.size() - 1;

            int mid = 0;
            // A[low] > A[high] ensure the pivot is between low and high
            while(low < high && A[low] > A[high])
            {   
                //if there only two elements, just return A[high], find pivot
                if(low + 1 == high)
                    return A[high];

                mid = (low + high)/2;

                //cout << "low\t" << low<< endl; 
                //cout << "high\t" << high << endl; 
                //cout << "mid\t" << mid<< endl; 

                if(A[low] < A[mid])//pivot is in bottom half, mid can't be lowest
                {   
                    low = mid + 1;
                }   
                else //pivot is in first half, mid may be lowest
                {   
                    high = mid;
                }   
            }   

            // this case: vecotr A is Sorted Array , not rotated, so just return A[low]
            return A[low];
        }   
};

 

 

略加修改,使其成为https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/的基础

        int findMin(vector<int>& A)
        {   
            int low = 0;
            int high = A.size() - 1;

            int mid = 0;
            // A[low] > A[high] ensure the pivot is between low and high
            while(low < high && A[low] > A[high])
            {   

                mid = (low + high)/2;

                //cout << "low\t" << low<< endl; 
                //cout << "high\t" << high << endl; 
                //cout << "mid\t" << mid<< endl; 

                if(A[low] < A[mid])//pivot is in bottom half, mid can't be lowest
                {   
                    low = mid + 1;
                }   
                else if(A[low] == A[mid])//there only two elements
                {   
                    //if there only two elements, just return A[high], find pivot
                    return A[high];
                }   
                else //pivot is in first half, mid may be lowest
                {   
                    high = mid;
                }   
            }   

            // this case: vecotr A is Sorted Array , not rotated, so just return A[low]
            return A[low];
        }

 

posted @ 2015-06-15 12:29  穆穆兔兔  阅读(231)  评论(0编辑  收藏  举报