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的更多相关文章

  1. 【LeetCode】162. Find Peak Element 解题报告(Python)

    [LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...

  2. 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] ≠ ...

  3. LeetCode 162. Find Peak Element (找到峰值)

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  4. (二分查找 拓展) 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 ...

  5. [LeetCode] 162. Find Peak Element 查找峰值元素

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  6. 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] ≠ ...

  7. ✡ 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] ≠ ...

  8. 【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 ...

  9. leetcode 162 Find Peak Element(二分法)

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  10. 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[ ...

随机推荐

  1. ssl多人多附件多格式邮件发送

    package com.dfmy.util; import java.io.File; import java.security.Security; import java.util.ArrayLis ...

  2. MWPhotoBrowser.bundle: bundle format unrecognized, invalid, or unsuitable

    今天在github下载了MWPhotoBrowser的demo想跑一下,却发现报了MWPhotoBrowser.bundle: bundle format unrecognized, invalid, ...

  3. (转)linux chattr lsattr 命令

    转http://www.ha97.com/5172.html PS:有时候你发现用root权限都不能修改某个文件,大部分原因是曾经用chattr命令锁定该文件了.chattr命令的作用很大,其中一些功 ...

  4. Codeforces1238F. The Maximum Subtree(树形dp)

    题目链接:传送门 思路: 题意说用线段的相交作为边,来构造树,所以不存在大于等于3个的线段两两相交,否则会构成环.因而构造出的树中,每个点最多只会与2个度大于1的节点相邻. 不妨把1设为树根,用deg ...

  5. Django学习系列8:django测试客户端

    """向浏览器返回真正的HTML响应,添加一个新的测试方法""" from django.test import TestCase from ...

  6. centos7 远程桌面连接到xfce桌面

    1 安装xfce $ sudo yum install -y epel-release $ sudo yum groupinstall -y "Xfce" $ sudo reboo ...

  7. 如何连接虚拟的OneNote打印机

    include <Windows.h> #include <iostream> int main() { HANDLE handle = CreateFile(L"O ...

  8. Jmeter性能测试--自己看到的博客收集

    性能测试的场景:https://www.cnblogs.com/little-little-bai/p/10338156.html

  9. HTML自定义radio单选按钮(纯css版,样式可以随意改变)

    html: <div> <input id="item1" type="radio" name="item" value= ...

  10. 循环结构——for语句、seq语句、while语句、break语句

    1.for语句: 运行结果: 2.seq命令生成整数序列: 3.while语句: 执行结果: 4.break语句: break语句是正常结束之前退出当前循环. 执行结果: 5.continue语句: ...