Class Solution

java.lang.Object
g0101_0200.s0162_find_peak_element.Solution

public class Solution extends Object
162 - Find Peak Element.<p>Medium</p> <p>A peak element is an element that is strictly greater than its neighbors.</p> <p>Given an integer array <code>nums</code>, find a peak element, and return its index. If the array contains multiple peaks, return the index to <strong>any of the peaks</strong>.</p> <p>You may imagine that <code>nums[-1] = nums[n] = -\u221e</code>.</p> <p>You must write an algorithm that runs in <code>O(log n)</code> time.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1,2,3,1]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> 3 is a peak element and your function should return the index number 2.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,2,1,3,5,6,4]</p> <p><strong>Output:</strong> 5</p> <p><strong>Explanation:</strong> Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 1000</code></li> <li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li> <li><code>nums[i] != nums[i + 1]</code> for all valid <code>i</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • findPeakElement

      public int findPeakElement(int[] nums)