75. Find Peak Element

Code(Language:C++)
class Solution {
public:
    /**
     * @param A: An integers array.
     * @return: return any of peek positions.
     */
    int findPeak(vector<int> &A) {
        // write your code here
        const int size = A.size();
        int start = 0; 
        int end = size - 1; 
        while(start + 1 < end){
            int mid = start + (end - start) / 2; 
            if(A[mid] > A[mid - 1]){
                start = mid; 
            }
            else{
                end = mid; 
            }
        }
        if(A[start] > A[end]){
            return start;
        }
        else{
            return end;
        }
    }
};

Comments

Popular posts from this blog

算法的比较