[LeetCode]Find Peak Element

public class Solution {
    public int findPeakElement(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        while (left + 1 < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] > nums[mid - 1] && nums[mid] < nums[mid + 1]) {
                left = mid;
            } else if (nums[mid] < nums[mid - 1] && nums[mid] > nums[mid + 1]) {
                right = mid;
            } else if (nums[mid] < nums[mid - 1] && nums[mid] < nums[mid + 1]) {
                right = mid;
            } else {
                return mid;
            }
        }
        return nums[left] > nums[right] ? left : right;
    }
}

 

posted @ 2015-12-03 15:44  Weizheng_Love_Coding  阅读(117)  评论(0编辑  收藏  举报