LeetCode Find Peak Element
原题链接在这里:https://leetcode.com/problems/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.
题解:
二分法, 如何判断接下来应该找左边还是右边呢。依据其实是比较nums[m] 和 nums[m+1], 若是nums[m] < nums[m+1], peak 一定会出现在mid 右边,不包括mid.
反之peak 就会出现在mid 左边, 但要包括mid.
Time Complexity: O(logn). Space: O(1).
AC Java:
class Solution {
public int findPeakElement(int[] nums) {
if(nums == null || nums.length == 0){
return -1;
}
int l = 0;
int r = nums.length - 1;
while(l < r){
int mid = l + (r-l)/2;
if(nums[mid] < nums[mid+1]){
l = mid+1;
}else{
r = mid;
}
}
return r;
}
}
类似Peak Index in a Mountain Array.
LeetCode Find Peak Element的更多相关文章
- [LeetCode] Find Peak Element 求数组的局部峰值
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode Find Peak Element [TBD]
说要写成对数时间复杂度,算了想不出来,写个O(n)的水了 class Solution { public: int findPeakElement(const vector<int> &a ...
- LeetCode Find Peak Element 找临时最大值
Status: AcceptedRuntime: 9 ms 题意:给一个数组,用Vector容器装的,要求找到一个临时最高点,可以假设有num[-1]和num[n]两个元素,都是无穷小,那么当只有一个 ...
- LeetCode: Find Peak Element 解题报告
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- [LeetCode] Find Peak Element 二分搜索
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- Lintcode: Find Peak Element
There is an integer array which has the following features: * The numbers in adjacent positions are ...
- LeetCode OJ 162. Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode 162. Find Peak Element (找到峰值)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...
随机推荐
- FireFox火狐浏览器与IE兼容问题 - 透明滤镜 DIV滚动条
问题一:最简单的鼠标移过手变型的css要改了 cursor:pointer;/*FireFox(火狐)不支持cursor:hand*/ dw8下面自动出来的也没有hand这个属性了,标准的是point ...
- 关于后台管理linkbutton按钮几个重要属性的理解
<asp:LinkButton ID="lkbtnDelete" runat="server" CausesValidation="False& ...
- 用Git进行协同开发
用Git进行协同开发 问题场景描述 常常会遇到这样的协同场景:后台的同事和前端的同事需要共同开发一个新功能,而他们的代码相互依赖,所以需要不停地更新各自的代码进行联调. 对于这种场景,最简单的方式就是 ...
- Medical Image Processing Conference and Journal 医学图像处理会议与期刊
会议: Information Processing in Medical Imaging,IPMI IPMI2013 International Conference on Medical Imag ...
- JS - 超强大文本动画插件Textillate.js
http://www.yyyweb.com/demo/textillate/ Textillate.js AsimplepluginforCSS3textanimations.
- java中实现链表(转)
分析: 上述节点具备如下特征: 1. 每个节点由两部分组成(存储信息的字段,存储指向下一个节点的指针) 2. 节点之间有着严格的先后顺序. 3. 单链表节点是一种非线性的结构,在内存中不连续分配空间. ...
- while,do while和for循环语句的用法
一.while的用法 //循环 int i = 10; while(i > 0){ if(i==8) {i--; continue;//跳过 } System.out.println(--i); ...
- java--测体重练习
public class tz{ public static void main(String[] args){ int sg=165,tz=52;bz = sg-115 if (tz-bz>3 ...
- Ubuntu 开启 Crontab 计划任务日志
Ubuntu 下的 Crontab 日志功能不是自动开启的.开启日志功能的步骤是: ① 修改 rsyslog sudo vim /etc/rsyslog.d/-default.conf 找到: #cr ...
- jQuery 实现带下拉提示且自动填充的邮箱
/* ** 本文例子所引用的jQuery版本为 jQuery-1.8.3.min.js ** Author:博客园小dee*/ 本文用 jQuery 实现一个用户输入字符时出现能够提示邮箱后缀名的下拉 ...