s4553711
11/29/2017 - 3:05 PM

162.cpp

class Solution {
public:
    int findPeakElement(vector<int>& nums) {
        int n = nums.size();
        if (n == 1) {
            return 0;
        }
        int start = 0, end = n - 1, mid = 0;
        while(start <= end) {
            mid = start + (end - start)/2;
            if ((mid == 0 || nums[mid] >= nums[mid-1]) && (mid == n - 1 || nums[mid] >= nums[mid+1] )) {
                return mid;
            } else if (mid > 0 && nums[mid - 1] > nums[mid]) {
                end = mid - 1;
            } else {
                start = mid + 1;
            }
        }
        return mid;
    }
};