162. Find Peak Element

A peak element is an element that is greater than its neighbors.

Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.

The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.

You may imagine that num[-1] = num[n] = -∞.

For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.

对于一个数组,如果它的形式是[1,2........5,4]那中间一定存在我们要找的峰值元素。
所以我们取数组中心mid,和mid+1处的值,看谁大,mid大就意味着左边有峰值,mid+1大就意味着右边有峰值。
当然,顺序搜索数组也是可以的。

var findPeakElement = function(nums) {
    // var num = nums.length;
    // if (num===1)
    //     return 0;
    // for(var i = 1;i < num;i++) {
    //     if (nums[i]

你可能感兴趣的:(162. Find Peak Element)