75. Find Peak Element 【medium】

There is an integer array which has the following features:

  • The numbers in adjacent positions are different.
  • A[0] < A[1] && A[A.length - 2] > A[A.length - 1].

We define a position P is a peek if:

A[P] > A[P-1] && A[P] > A[P+1]

Find a peak element in this array. Return the index of the peak.

Notice
  • It's guaranteed the array has at least one peak.
  • The array may contain multiple peeks, find any of them.
  • The array has at least 3 numbers in it.
Example

Given [1, 2, 1, 3, 4, 5, 7, 6]

Return index 1 (which is number 2) or 6 (which is number 7)

Challenge

Time complexity O(logN)

解法一:

 class Solution {
public:
/*
* @param A: An integers array.
* @return: return any of peek positions.
*/
int findPeak(vector<int> &A) {
if (A.empty()) {
return -;
} int start = ;
int end = A.size() - ; while (start + < end) {
int mid = start + (end - start) / ; if (A[mid] > A[mid - ]) {
if (A[mid] > A[mid + ]) {
return mid;
}
else {
start = mid;
}
}
else {
if (A[mid] > A[mid + ]) {
end = mid;
}
else {
start = mid;
}
}
} return -;
}
};

分类讨论。

解法二:

 class Solution {
/**
* @param A: An integers array.
* @return: return any of peek positions.
*/
public int findPeak(int[] A) {
// write your code here
int start = , end = A.length-; // 1.答案在之间,2.不会出界
while(start + < end) {
int mid = (start + end) / ;
if(A[mid] < A[mid - ]) {
end = mid;
} else if(A[mid] < A[mid + ]) {
start = mid;
} else {
end = mid;
}
}
if(A[start] < A[end]) {
return end;
} else {
return start;
}
}
}

参考http://www.jiuzhang.com/solution/find-peak-element/的解法,此法更简单。

75. Find Peak Element 【medium】的更多相关文章

  1. 159. Find Minimum in Rotated Sorted Array 【medium】

    159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...

  2. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  3. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  4. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  5. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  6. 27. Remove Element【easy】

    27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...

  7. 61. Search for a Range【medium】

    61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...

  8. 62. Search in Rotated Sorted Array【medium】

    62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...

  9. 74. First Bad Version 【medium】

    74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...

随机推荐

  1. 【莫比乌斯反演】BZOJ2920-YY的GCD

    [题目大意] 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对. [思路] 太神了这道题……蒟蒻只能放放题解:戳,明早再过来看看 ...

  2. mysql获取分类数量

    1.sql <select id="getTypeNum" resultType="TypeNum" > select count(*) as al ...

  3. flask 开发环境搭建

    window下: 1)安装python 2)安装pip 3)使用pip install flask 如果成功安装使用pip list 既可以查看到flask的版本 ubuntu下的环境搭建 同样地使用 ...

  4. 学习Microsoft SQL Server 2008技术内幕:T-SQL语法基础--第4章

    第4章 子查询 4.2.1 Exist 谓语: use TSQLFundamentals2008 select * from Sales.Customers as C where c.country= ...

  5. mq

    同时每个 Broker 与NameServer 集群中的所有节点建立长连接,定时注册 Topic 信息到所有 NameServer 中. Producer 与 NameServer 集群中的其中一个节 ...

  6. nullptr 与 constexpr

    nullptr   nullptr出现的目的自然是替换NULL的低位.C++可能会将NULL.0视为同一种东西.这取决于编译器是如何定义的,有的编译器定义NULL为 ( (void * )0) ,有的 ...

  7. leetcode练习之No.7------ 翻转整数reverse_integer

    原文地址:http://www.niu12.com/article/48 git地址:git@github.com:ZQCard/leetcode.git 给定一个 32 位有符号整数,将整数中的数字 ...

  8. 性能测试之工具对比-ngrinder jmeter loadunner及ngrinder安装使用方法

    参考:https://blog.csdn.net/bear_w/article/details/78366078

  9. php之防注入程序绕过浅谈

    <?php/*判断传递的变量是否含有非法字符如:$_POST/$_GET功能:SQL防注入系统*/ //屏蔽错误提示error_reporting(7); //需要过滤的字符 $ArrFiltr ...

  10. fl2440 platform总线led字符设备驱动

    首先需要知道的是,设备跟驱动是分开的.设备通过struct device来定义,也可以自己将结构体封装到自己定义的device结构体中: 例如:struct platform_device: 在inc ...