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 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.
双指针:
class Solution {
public:
int findPeakElement(vector<int>& a) {
int i = 0;
int j = a.size() - 1;
if (j==0) return 0;
while(i<j) {
if (a[i]<a[j]) {
i++;
} else {
j--;
}
}
return i;
}
};
class Solution {
public int findPeakElement(int[] nums) {
int n = nums.length;
int lo = 0;
int hi = n - 1;
while(hi>lo){
int mid = lo + (hi - lo)/2;
if(nums[mid]<nums[mid+1])
lo = mid + 1;
else
hi = mid;
}
return lo;
}
}
162. 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] ≠ ...
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
- (二分查找 拓展) 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 ...
- [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
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
- ✡ 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] ≠ ...
- 【LeetCode】162. Find Peak Element (3 solutions)
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- 【刷题-LeetCode】162 Find Peak Element
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- leetcode 162 Find Peak Element(二分法)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
随机推荐
- Rollup 与 webpack的区别
特性: webpack 拆分代码, 按需加载: Rollup 所有资源放在同一个地方,一次性加载,利用 tree-shake 特性来剔除项目中未使用的代码,减少冗余,但是webpack2已经逐渐支持t ...
- socket网络编程【python】
转自:http://www.jb51.net/article/19751.htm socket 是网络连接端点. 一.网络知识的一些介绍 socket 是网络连接端点.例如当你的Web浏览器请求www ...
- /etc/sysctl.conf
/etc/sysctl.conf 用于控制内核相关的配置参数,而且它的内容全部是对应于 /proc/sys/ 这个目录的子目录及文件 [root@localhost ~]$ ll /proc/sys ...
- canvas一:基本认识
最近弄数据库弄得头大,想着没事整理一下画布canvas吧,毕竟canvas用途广泛,是html游戏开发必不可少的一环,也是h5新特性中的重中之重 首先canvas是一个html标签,可以给他设置一些c ...
- Mybatis中的foreach
<delete id="deleteByParam"> DELETE FROM YZ_SECURITIES_CURRENCY WHERE ID IN <forea ...
- iOS - xib中关于拖拽手势的潜在错误
iOS开发拓展篇—xib中关于拖拽手势的潜在错误 一.错误说明 自定义一个用来封装工具条的类 搭建xib,并添加一个拖拽的手势. 主控制器的代码:加载工具条 封装工具条以及手势拖拽的监听事件 此时运行 ...
- Linux下多任务间通信和同步-mmap共享内存
Linux下多任务间通信和同步-mmap共享内存 嵌入式开发交流群280352802,欢迎加入! 1.简介 共享内存可以说是最有用的进程间通信方式.两个不用的进程共享内存的意思是:同一块物理内存被映射 ...
- Delphi的打开文件对话框-TOpenDialog
1.TOpenDialog组件的典型用法 “打开”对话框是用TOpenDialog组件实现的,TOpenDialog组件是非可视组件. Filter属性用于设置文件过滤器,让对话框只列出特定类型的文件 ...
- 注册和删除Apache服务器的方法
Apache服务器的安装和卸载方法 下载Apache安装包 将Apache文件夹存在桌面或其他盘,输入cmd打开命令提示行 安装步骤:进入Apache安装目录下的bin目录: cd C:\Us ...
- 深入浅出Docker(六):像谷歌一样部署你的应用
1.概述 谷歌发起的开源项目从来都是广受技术圈的关注和讨论,本文将介绍的就是最新的容器编排管理系统Kubernetes.Kubernetes开源项目版本更新频繁,对于初次使用者来说其定义大量的技术术语 ...