LeetCode 162 Find Peak Element
Problem:
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.
Your solution should be in logarithmic complexity.
Summary:
找到数组中的局部最大数。
Solution:
1. 顺序查找:最直接的方法,复杂度为O(n)
- class Solution {
- public:
- int findPeakElement(vector<int>& nums) {
- int len = nums.size();
- if (len == ) {
- return ;
- }
- for (int i = ; i < len; i++) {
- if (!i && nums[i] > nums[i + ] ||
- i == len - && nums[i] > nums[i - ] ||
- nums[i] > nums[i - ] && nums[i] > nums[i + ]) {
- return i;
- }
- }
- return -;
- }
- };
2. 二分查找:首先找到整体的中间值m,若m符合局部最大条件则返回m,否则若nums[m - 1] > nums[m]则在[0, m - 1]中查找。因为数组左边和右边为负无穷,所以在这种情况下[0, m - 1]中一定存在一个局部最大值。这种方法复杂度为O(logn)。
- class Solution {
- public:
- int findPeakElement(vector<int>& nums) {
- int len = nums.size();
- if (len == ) {
- return ;
- }
- int l = , r = len - ;
- while (l <= r) {
- int m = (l + r) / ;
- if ((!m || nums[m] >= nums[m - ]) &&
- (m == len - || nums[m] >= nums[m + ])) {
- return m;
- }
- if (m && nums[m] < nums[m - ]) {
- r = m - ;
- }
- else {
- l = m + ;
- }
- }
- return -;
- }
- };
二分查找的简略写法:
- class Solution {
- public:
- int findPeakElement(vector<int>& nums) {
- int len = nums.size();
- if (len == ) {
- return ;
- }
- int l = , r = len - ;
- while (l < r) {
- int m = (l + r) / ;
- if (nums[m] > nums[m + ]) {
- r = m;
- }
- else {
- l = m + ;
- }
- }
- return r;
- }
- };
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 查找峰值元素
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(二分法)
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[ ...
- LeetCode——162. Find Peak Element
一.题目链接: https://leetcode.com/problems/find-peak-element/ 二.题目大意: 给定一个长度为N的一维数组,数组是无序的,要求找到数组中的极大值(或局 ...
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
随机推荐
- block
不使用__block修饰: ; self.myBlock = ^(){ NSLog(@"block num == %d",num); }; num = ; self.myBlock ...
- django 补充篇
from验证 django中的Form一般有两种功能: 输入html-----------不能你自己写一些标签,而帮你自动生成 验证用户输入-------将用户验证信息保存起来,可以传到前端 # !/ ...
- Linux编译工具:gcc入门
1. 什么是gcc gcc的全称是GNU Compiler Collection,它是一个能够编译多种语言的编译器.最开始gcc是作为C语言的编译器(GNU C Compiler),现在除了c语言,还 ...
- 真正解决vbox不能为虚拟电脑打开一个新任务的解决方法
今天小编电脑上出现了一个问题,如上图,经过了一个朋友的帮助终于问题解决了,解决方法: 在每一个中把 红框中打上勾号即可
- 一篇对iOS音频比较完善的文章
转自:http://www.cnblogs.com/iOS-mt/p/4268532.html 感谢作者:梦想通 前言 从事音乐相关的app开发也已经有一段时日了,在这过程中app的播放器几经修改我也 ...
- iOS静态库开发中对Bitcode的支持
1.bitcode bitcode是LLVM编译器将C/C++/OC/Swift等前端变成语言编译成多种不同芯片上的机器指令过程中的中间代码.并且这个中间代码是CPU无关的. 原本我们的APP里要包含 ...
- 获取URL中的参数
function GetQueryString(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)( ...
- 常用HTTP状态码和CURL 000问题
最近在测试CDN服务质量问题,测试过程中返回了一些不同的状态码,当然有一些常用的,也有一些不常用的.最奇葩的是在使用curl命令的时候出现000状态码,问了很多同事,对这个000的反应跟新事物是的 ...
- Java中FilterInputStream和FilterOutputStream的用法
FilterInputStream FilterInputStream 的作用是用来"封装其它的输入流,并为它们提供额外的功能".它的常用的子类有BufferedInputStre ...
- easyui如何动态改变列的编辑属性
动态改变列的编辑属性 var tt=$('#dg').datagrid('getColumnOption', 'yearContent'); //通过列名获得此列 tt.editor={type:'t ...