leetcode-mid-sorting and searching-162. Find Peak Element
mycode 54.81%
class Solution(object):
def findPeakElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 0:
return None
if len(nums) == 1 or nums[0]>nums[1] :
return 0
if nums[-1] > nums[-2]:
return len(nums)-1 for i in range(1,len(nums)-1):
if nums[i] > nums[i-1] and nums[i] > nums[i+1]:
return i
参考:
思路:题目中左右为负无穷这个条件可以用起来,把原列表扩增以下就可以把两端的情况合并在一个for循环讨论啦
import math
class Solution(object):
def findPeakElement(self, nums):
"""
:type nums: List[int]
:rtype: int
""" nums = [-float('inf')] + nums + [-float('inf')]
for i in range(1, len(nums)-1):
if nums[i-1] < nums[i] > nums[i+1]:
return i-1
leetcode-mid-sorting and searching-162. Find Peak Element的更多相关文章
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
- 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 ...
- [LeetCode] 162. Find Peak Element 查找峰值元素
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- Java for 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 --------- 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(二分法)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode 162.Find Peak Element(M)(P)
题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...
随机推荐
- sql server 获取随机数函数RAND()和RAND(x)
--RAND(x)返回一个随机浮点值v,范围在0~1之间(即0<=v<=1.0) --若指定一个整数参数x,则它被用作种子值,使用相同的种子数将产生重复序列.如果同一种子值多次调用RAND ...
- 10.css3动画--过渡动画--trasition
Transition简写属性. Transition-property规定应用过渡的css属性的名称. . Transition-timing-function过渡效果的时间曲线,默认是ease. L ...
- Intellij IDEA 最全实用快捷键整理
正文前: 1. IDEA内存优化(秒开的快感!!) 因机器本身的配置而配置: \IntelliJ IDEA8\bin\idea.exe.vmoptions // (根据你的配置变大!!) ------ ...
- js 性能优化 - web worker
当在 HTML 页面中执行脚本时,页面的状态是不可响应的,直到脚本已完成. web worker 是运行在后台的 JavaScript,独立于其他脚本,不会影响页面的性能. 您可以继续做任何愿意做的事 ...
- Spring Boot任务(定时,异步,邮件)
一.定时任务 开启定时任务(在Spring Boot项目主程序上添加如下注解) @EnableScheduling //开启定时任务的注解 创建定时任务(创建一个Service如下) @Service ...
- mongoose 开源http库
Mongoose是一个用C编写的网络库.它为客户端和服务器模式实现TCP,UDP,HTTP,WebSocket,CoAP,MQTT的事件驱动的非阻塞API. 设计理念: Mongoose有三个基本的数 ...
- Linux磁盘分区与lvm逻辑卷
硬盘接口的种类分四类:(价格由低到高) IDE SATA硬盘:别名串口硬盘,具有较强的纠错能力. SCSI硬盘:即采用SCSI接口的硬盘,SCSI接口具有应用范围广,多任务,带宽大,CPU占用率低. ...
- DCGAN生成式对抗网络--keras实现
本文针对cifar10 图集进行了DCGAN的复现. 其中库中的SpectralNormalizationKeras需添加至python环境中 该篇代码如下: from keras import ba ...
- jquery 未来元素事件示例 on() delegate() live()
jquery 1.7版后建议使用on() $(document).on("click","#green",function(){$(this).after('& ...
- xmlns, xmlns:xsi, xsi:schemaLocation 解释
xmlns, xmlns:xsi, xsi:schemaLocation 解释 xmlnsxsischemaLocation 我们在写 xml 文件时,尤其是 spring .mybatis 的配置文 ...