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.
public class Solution { public int findPeakElement(int[] nums) { if (nums == null || nums.length == 0) { return -1; } if (nums.length == 1 || nums[0] > nums[1]) { return 0; } if (nums[nums.length-1] > nums[nums.length-2]) { return nums.length - 1; } int left = 1; int right = nums.length - 2; int mid = 0; while (left < right) { mid = (left + right) / 2; if (nums[mid] < nums[mid-1]) { right = mid - 1; } else if (nums[mid] < nums[mid+1]) { left = mid + 1; } else { return mid; } } return left; } }
相关推荐
java java_leetcode题解之Find Peak Element
javascript js_leetcode题解之162-find-peak-element.js
python python_leetcode题解之162_Find_Peak_Element.py
从标题和描述中可以看到,本文主要关注于LeetCode原题的总结,涵盖了162个题目,包括Find Peak Element、Isomorphic Strings、Group Shifted Strings等,这些题目都是Google Onsite面经中的常见题目。 下面将对这些...
lru cache leetcode Coding-Interview ...Peak Element First Bad Version Valid Perfect Square 二叉树查找/BST查找 Closest Binary Search Tree Value 二叉树查找/二叉树第K个 Kth Smallest Element In A
1. Introduction 2. Array i. Remove Element ii. Remove Duplicates from Sorted Array iii....iv....v....vi....vii. Find Minimum in Rotated Sorted Array viii....ix....x....xi....xii....xiii....xiv. Find Peak Element
peak element 34: find first and last position of element in sorted array 300: longest increasing subsequence hard 154: find minimum in rotated sorted array ii 315: count of smaller numbers after self ...
- Find Peak Element(寻找峰值元素) - Search in Rotated Sorted Array(在旋转排序数组中搜索) - Search in Rotated Sorted Array II(在旋转排序数组中搜索II) - Find Minimum in Rotated Sorted Array...
element(162).swift Leetcode\find重复的数字(287).swift Leetcode\friend circles(547).swift Leetcode\gas station(134)。 swift Leetcode\group anagrams(49).swift Leetcode\group 给定他们所属的组大小的人...
- Find Peak Element:在一个整数数组中找到一个峰值元素。 - Median of Two Sorted Arrays:两个排序数组的中位数。 - Sqrt(x):计算并返回x的平方根。 - Wood Cut:木材切割问题,给定木材长度,求可以切出的最大...
- **寻找峰值元素(Find Peak Element)**: 给定一个未排序的数组,找到一个“峰值”元素,即其值大于相邻元素。 ##### 位操作(Bit Manipulation) - **缺失数字(Missing Number)**: 给定一个包含[0, n]范围内所有...
- **查找峰值元素(Find Peak Element)** - 在一个一维数组中找到一个峰值元素。 - **旋转排序数组中的搜索(Search in Rotated Sorted Array)** - 在一个旋转过的排序数组中查找一个元素。 以上就是Lintcode算法...
Find Peak Element.Binary search,需要比较nums[mid]和nums[mid+1]. 4.12 212题. Word Search II. 用trie tree存word list,然后dfs. 4.11 788题. Rotated Digits.转换成字符串,时间复杂度O(nlogn).还可以用dp,dp...
- **查找峰值元素(Find Peak Element)**: 在一个无序数组中找到任意一个局部最大值。 - **在旋转排序数组中搜索/Search in Rotated Sorted Array**: 在一个被旋转过的排序数组中搜索给定数字。 - **在旋转排序数组中...
find-peak-element longest-common-subsequence longest-consecutive-sequence max-area-of-island next-greater-element-ii serialize-and-deserialize-binary-tree subarray-sum-equals-k binary-tree-preorder-...
We also find that a larger size of inner cladding air holes will lead to a more pronounced second peak in the BGS. On the other hand, the size of the outer cladding has nearly no effect on the
search_box = driver.find_element_by_id("kw") search_box.send_keys("python") # 模拟按下回车键 search_box.send_keys(Keys.RETURN) # 等待页面加载完成 # 进行其他操作... # 关闭浏览器 driver.quit() ``` ###...