[LeetCode][JavaScript]Find Peak Element

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.

click to show spoilers.

Note:

Your solution should be in logarithmic complexity.

https://leetcode.com/problems/find-peak-element/



提示说要求指数级的复杂度,我就想不能够吧...

原来是先入为主了,没说求最高的峰顶,只要是峰顶,随便哪个都可以Orz。

我原以为是有多个最高的峰顶,输出哪个都可以,原来要求这么随便,这样真的没问题吗。

于是就可以二分了,中点无非就是四种情况。

1.中点是峰顶,直接输出,收工。

2.中点在一条上坡里,峰顶肯定在右边。

3.中点在一条下坡里,峰顶肯定在左边。

4.中点是峰谷,取左边和右边都可以...

 1 /**
 2  * @param {number[]} nums
 3  * @return {number}
 4  */
 5 var findPeakElement = function(nums) {
 6     var m = 0, n = nums.length - 1;
 7     var middle, left, right;
 8     while(m <= n){
 9         middle = parseInt((m + n) / 2);
10         left = nums[middle - 1] ? nums[middle - 1] : -Infinity;
11         right = nums[middle + 1] ? nums[middle + 1] : -Infinity;
12         if(nums[middle] > left && nums[middle] > right){
13             return middle;
14         }else if(left < nums[middle]&& nums[middle] < right){
15             m = middle + 1;
16         }else{
17             n = middle - 1;
18         }
19     }
20     return nums[m] > nums[n] ? nums[m] : nums[n];
21 };
时间: 2024-07-29 07:27:53

[LeetCode][JavaScript]Find Peak Element的相关文章

LeetCode:Find Peak Element - 寻找一个数组内的顶点

1.题目名称 Find Peak Element(寻找一个数组内的顶点) 2.题目地址 https://leetcode.com/problems/find-peak-element/ 3.题目内容 英文: 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

LeetCode 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 fi

Leetcode 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 fi

leetcode[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 fi

【LeetCode】Find Peak Element (3 solutions)

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. You may imagine that num[-1] = num[n] = -∞. For example, in array [1, 2, 3, 1],

【leetcode】Find Peak Element

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

leetcode 【 Find Peak Element 】python 实现

题目: 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 i

LeetCode[Array]: 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. You may imagine that num[-1] = num[n] = -∞. For example, in array [1, 2, 3, 1], 3 is a peak eleme

leetcode 162. Find Peak Element --------- java

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 fi