[LeetCode] 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.
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 查找峰值元素的更多相关文章
- 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 --------- 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
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
- 162 Find Peak Element 寻找峰值
峰值元素是指其值大于左右相邻值的元素.给定一个输入数组,其中 num[i] ≠ num[i+1],找到峰值元素并返回其索引.数组可能包含多个峰值,在这种情况下,返回到任何一个峰值所在位置都可以.你可以 ...
- 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[ ...
- 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(二分法)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode Problem 169: Majority Element查找多数元素
描述:Given an array of size n, find the majority element. The majority element is the element that app ...
随机推荐
- php string常用函数
<?php $a[]='a'; $a[]='b'; $a[]='C'; echo "</br>"; /* implode — 将一个一维数组的值转化为字符串 说明 ...
- c++控制输出的字体颜色
SetConsoleTextAttribute(参数表).SetConsoleTextAttribute()函数是一个API设置字体颜色和背景色的函数.参数表中使用两个属性(属性之间用,隔开).Get ...
- NOIP2017 PJ 跳房子 —— 单调队列优化DP
题目描述 跳房子,也叫跳飞机,是一种世界性的儿童游戏,也是中国民间传统的体育游戏之一.跳房子的游戏规则如下: 在地面上确定一个起点,然后在起点右侧画n个格子,这些格子都在同一条直线上.每个格子内有一个 ...
- Laravel —— 路由问题
在 Laravel 中,路由是项目的起点. 下面总结一些路由中常见的问题. 一.路由 404 是因为配置文件没有开启重定向模块,可以通过下面的操作解决. 1.php.ini 开启 openssl 模块 ...
- MySql数据封装操作类
1.先引用MySQL的DLL文件 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
- [转贴] bu AU3脚本录制工具(软件自动化安装的最简便的方法)
http://www.autoitx.com/thread-15419-1-1.html 1,打开一个.au3的文档或者新建一个.au3的文档,用SciTE编辑; 2,按下ALT+F6,弹出下面的对话 ...
- Omnibus-ctl: What is it and what can it do for you?
转自:https://blog.chef.io/2015/05/26/omnibus-ctl-what-is-it-and-what-can-it-do-for-you/ Are you buildi ...
- android 自己制作Jar包 和 修改 现成的 Jar包文件
先看如何创建自己的 Jar 包 里面随便写个方法 public int add(int a,int b){ return (a+b); } task makeJar(type: Copy) { del ...
- Bzoj 1927: [Sdoi2010]星际竞速(网络流)
1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec Memory Limit: 259 MB Description 10年一度的银河系赛车大赛又要开始了.作为全银河最盛大 ...
- 微信小程序敏捷开发实战
wxml->wcc编译->javascript 用户javascript-> wawebview->view 小程序原理 微信 小程序-> webview appserv ...