Difficult:Medium
題目
A peak element is an element that is strictly greater than its neighbors.
Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.
You may imagine that nums[-1] = nums[n] = -∞. In other words, an element is always considered to be strictly greater than a neighbor that is outside the array.
You must write an algorithm that runs in O(log n) time.
翻譯
峰值元素是嚴格大於其鄰居的元素。
給定一個索引為 0 的整數數組 nums,找到一個峰值元素,並返回它的索引。如果數組包含多個峰,則返回任何峰的索引。
你可以想像 nums[-1] = nums[n] = -∞。換句話說,一個元素總是被認為嚴格大於數組外部的鄰居。
您必須編寫一個在 O(log n) 時間內運行的算法。
範例
Example 1:
1 | Input: nums = [1,2,3,1] |
Example 2:
1 | Input: nums = [1,2,1,3,5,6,4] |
解題思路
- 若小於長度2返回null
- 駝峰分為三種
2-1 左大於右,及只有左跟右2個,
2-2 中大於左和右
2-3右大於左,及只有左跟右2個 - 達成以上條件即可為駝峰
程式碼
1 | var findPeakElement = function(nums) { |