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.

click to show spoilers.

Note:

Your solution should be in logarithmic complexity.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

给一个数组,寻找里面的峰值元素。峰值是比它两边的元素都大。

提示了用log的时间复杂度,所以考虑用二分法Binary Search。

规律一:如果nums[i] > nums[i+1],则在i之前一定存在峰值元素
规律二:如果nums[i] < nums[i+1],则在i+1之后一定存在峰值元素

参考:Orange橘子洲头

Java:

public class Solution {
public int findPeakElement(int[] nums) {
int left = 0, right = nums.length - 1;
while (left < right) {
int mid = (left + right) / 2;
if(nums[mid] < nums[mid + 1]) left = mid + 1;
else right = mid;
}
return left;
}
}   

Python:

class Solution(object):
def findPeakElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
left, right = 0, len(nums) - 1 while left < right:
mid = left + (right - left) / 2
if nums[mid] > nums[mid + 1]:
right = mid
else:
left = mid + 1 return left

C++:

class Solution {
public:
int findPeakElement(vector<int>& nums) {
int left = 0, right = nums.size() - 1; while (left < right) {
const auto mid = left + (right - left) / 2;
if (nums[mid] > nums[mid + 1]) {
right = mid;
} else {
left = mid + 1;
}
} return left;
}
};

    

All LeetCode Questions List 题目汇总

[LeetCode] 162. Find Peak Element 查找峰值元素的更多相关文章

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

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

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

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

  4. LeetCode 162 Find Peak Element

    Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...

  5. 162 Find Peak Element 寻找峰值

    峰值元素是指其值大于左右相邻值的元素.给定一个输入数组,其中 num[i] ≠ num[i+1],找到峰值元素并返回其索引.数组可能包含多个峰值,在这种情况下,返回到任何一个峰值所在位置都可以.你可以 ...

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

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

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

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

  9. LeetCode Problem 169: Majority Element查找多数元素

    描述:Given an array of size n, find the majority element. The majority element is the element that app ...

随机推荐

  1. 对字符串'//*[@]/div/p/a[1]/c[2]/a[3]/b'从右向左依次删除指定字符串

    import re s='//*[@]' a=s+'/div/p/a[1]/c[2]/a[3]/b' c=[1,2] b=a.split(s) #切割 c=b[1].split('/') #切割 d= ...

  2. 项目Alpha冲刺——测试

    作业描述 课程: 软件工程1916|W(福州大学) 作业要求: 项目Alpha冲刺(团队) 团队名称: 火鸡堂 作业目标: 完成项目Alpha冲刺 团队信息 队名:火鸡堂 队员学号 队员姓名 博客地址 ...

  3. centos7中,mysql连接报错:1130 - Host ‘118.111.111.111’ is not allowed to connect to this MariaDB server

    客户端连接报错 这个问题是因为用户在数据库服务器中的mysql数据库中的user的表中没有权限. 解决步骤 1.连接服务器: mysql -u root -p 2.看当前所有数据库:show data ...

  4. 《团队作业第三、四周》五阿哥小组Scrum 冲刺阶段---Day4

    <团队作业第三.四周>五阿哥小组Scrum 冲刺阶段---Day3 一.项目燃尽图 二.项目进展 20182310周烔今日进展: 主要任务一览:聊天软件主界面 20182330魏冰妍今日进 ...

  5. WORD添加批注(JAVA)

    import com.spire.doc.*;import com.spire.doc.documents.CommentMark;import com.spire.doc.documents.Com ...

  6. L1141

    一,看题 1,位于0格可移动到相邻得1格.位于1格可移动到相邻的0格上. 2,从某一格开始可以移动的格子数.(应该不能重复,否则不久循环了.那就意味着我们可以要标记喽?) 3 二,写题 1,你是一次一 ...

  7. 004——转载—Word2016“此功能看似已中断 并需要修复”问题解决办法

    解决办法如下: 在Win10系统上安装 Office 2016 之后,每次打开Word文档可能都会提示“很抱歉,此功能看似已中断,并需要修复,请使用Windows 控制面板中的“程序和功能”选项修复M ...

  8. java 值传递、引用传递

    class Demo02 { public static void main(String[] args) { int a=1; get(a);//值传递 System.out.println(a); ...

  9. linux下递归删除目录下所有exe文件---从删库到跑路篇

    linux下递归删除目录下所有exe文件 find . -name '*.exe' -type f -print -exec rm -rf {} \; (1) "." 表示从当前目 ...

  10. SIGIR2018 Paper Abstract Reading Notes (1)

    1.A Click Sequence Model for Web Search(日志分析) 更好的理解用户行为对于推动信息检索系统来说是非常重要的.已有的研究工作仅仅关注于建模和预测一次交互行为,例如 ...