[LeetCode] 162. Find Peak Element_Medium tag: Binary Search
A peak element is an element that is greater than its neighbors.
Given an input array nums
, where nums[i] ≠ nums[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 nums[-1] = nums[n] = -∞
.
Example 1:
Input: nums =[1,2,3,1]
Output: 2
Explanation: 3 is a peak element and your function should return the index number 2.
Example 2:
Input: nums =[
1,2,1,3,5,6,4]
Output: 1 or 5
Explanation: Your function can return either index number 1 where the peak element is 2,
or index number 5 where the peak element is 6.
Note:
Your solution should be in logarithmic complexity.
思路就是要么在上升(l = mid), 要么在下降(r = mid), 要么在谷底或者顶点(l = mid or r = mid) 都可以.最后判断A[l] < A[r] , return r else l
04/14/2019 update:
分四种情况: nums[mid - 1] ? nums[mid] ? nums[mid + 1]
1) < > , 峰值,直接返回
2)< <, 上升,取右边,因为nums[-1] = 负无穷
3)> >, 下降,取左边
4)> < , 低谷, 取右边, 可以将2与4合并
Code
class Solution:
def findPeakElement(self, nums):
l, r = 0, len(nums)-1 # 可以是0 or len(nums) -1
while l + 1 < r:
mid = l + (r-l)//2
if nums[mid] < nums[mid - 1]:
r = mid
elif nums[mid] < nums[mid + 1]:
l = mid
else:
l = mid # or r = mid
if nums[l] < nums[r]:
return r
return l
2) update
class Solution:
def findPeakElement(self, nums):
l,r = 0, len(nums) - 1
while l + 1 < r:
mid = l + (r - l)//2
if nums[mid - 1] < nums[mid] > nums[mid + 1]:
return mid
elif nums[mid - 1] > nums[mid] > nums[mid + 1]:
r = mid
else:
l = mid
return r if nums[r] > nums[l] else l
[LeetCode] 162. Find Peak Element_Medium tag: Binary Search的更多相关文章
- [LeetCode] 278. First Bad Version_Easy tag: Binary Search
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- [LeetCode] 69. Sqrt(x)_Easy tag: Binary Search
Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...
- [LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [LeetCode] 109. Convert Sorted List to Binary Search Tree 把有序链表转成二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [LeetCode] 704. Binary Search_Easy tag: Binary Search
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...
- [LeetCode] 153. Find Minimum in Rotated Sorted Array_Medium tag: Binary Search
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [LeetCode] 35. Search Insert Position_Easy tag: Binary Search
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search
Description Given a sorted array of n integers, find the starting and ending position of a given tar ...
随机推荐
- nginx 根据端口不同实现负载均衡
upstream www.abc.com { server www.mynginx.com:91; server www.mynginx.com:92; }server { listen 80; se ...
- ubuntu部署php7.1
先更新本机内置的程序. sudo apt-get updatesudo apt-get upgrade 安装PHP sudo add-apt-repository ppa:ondrej/php sud ...
- 笔记本串口连接IBM 小机
首先要有一根两头母的九针串口线&USB转串口线其次,配置波特率19200.数据位8.停止位1.无校验位.流控:Xon/Xoff P4是9600,P5 P6都是19200了( 默认是19200波 ...
- [No0000191]7种提高工作效率的Vim操作-Vim使用技巧(6)
Vim一直被认为是一种非常高效的文本编辑器,但是对于普通用户来说,很难在入门的时候就体会到Vim的所谓高效性. 本文介绍7种提高你工作效率和生产力的Vim使用技巧,主要集中在对某个文件范围内的特定目标 ...
- [No0000FB]C# 命名空间(Namespace)
命名空间的设计目的是提供一种让一组名称与其他名称分隔开的方式.在一个命名空间中声明的类的名称与另一个命名空间中声明的相同的类的名称不冲突. 定义命名空间 命名空间的定义是以关键字 namespace ...
- [No0000EC]C# 字符串(String)
在 C# 中,可以使用字符数组来表示字符串,但是,更常见的做法是使用 string 关键字来声明一个字符串变量.string 关键字是 System.String 类的别名. 创建 String 对象 ...
- sed中支持变量的处理方法
1.eval sed ’s/$a/$b/’ filename2.sed "s/$a/$b/" filename3.sed ’s/’$a’/’$b’/’ filename 4.sed ...
- int 4 bytes
http://waynewhitty.ie/blog-post.php?id=19 MySQL - INT(11) vs BIGINT(11) vs TINYINT(11) This seems to ...
- jquery validate强大的jquery表单验证插件
jquery validate的官方演示和文档地址: 官方网站:http://jqueryvalidation.org/ 官方演示:http://jqueryvalidation.org/files/ ...
- idea 在tomcat启动的时候发现控制台输出的是乱码
先前觉得是tomcat的问题,后来发现是idea的问题: 打开IntelliJ IDEA本地安装目录中bin文件夹下的idea.exe.vmoptions和idea64.exe.vmoptions这两 ...