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. Exercise01_11

    public class Population{ public static void main(String[] args){ int sum,s; s=365*5*24*60*60; sum=31 ...

  2. c pvr转存pvr.ccz格式 (转 http://www.cnblogs.com/howeho/p/3586379.html)

    pvr.ccz 是把pvr用zlib算法压缩后的图像格式,其优点是可以提升文件读取效率. 大多数情况下我们可以用一些工具来将pvr压缩到pvr.ccz ,下面提供一个c++方法来完成这个过程 int ...

  3. MR实现--矩阵乘法

    import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.io ...

  4. ListView控件(下)简单适配器

    (一) 1.效果图 2.activiy_main.xml <?xml version="1.0" encoding="utf-8"?> <Li ...

  5. linux-去重-uniq

    uniq : 默认(去重)  |  -d(显重)   |   -u(删重) 语法:uniq  [选项]  文件 选项 -c或--count 在每列旁边显示该行重复出现的次数 -d或--repeat 仅 ...

  6. 如何设断点????-----使用WinDbg调试SQL Server查询

    http://www.cnblogs.com/woodytu/p/4665427.html http://www.sqlservercentral.com/blogs/aschenbrenner/20 ...

  7. Android Studio Emulator 提示 “/dev/kvm is not found” 解决办法

    重新安装HAXM即可解决 1.确定已经安装HAXM SDK Manager -> Extras -> Intel x86 Emulator Accelerator (HAXM instal ...

  8. JSON.parse(str),JSON.stringify(a)

    1.JSON.parse(str),字符串转换成对象 var str = '{"name":"huangxiaojian","age":&q ...

  9. VSM(Virtual Storage Manager For Ceph)安装教程

    转载注明出处,陈小跑 http://www.cnblogs.com/chenxianpao/p/5770271.html 一.安装环境 OS:CentOS7.2 VSM:v2.1 released 二 ...

  10. python中单星号(*)和双星号(**)的使用

    1.单星号*:对元组解包:接收可变参数 2.双星号**:对字典解包:接收可变参数 python函数的五类参数类型及出现顺序:必选参数.默认参数.可变参数.命名关键字参数.关键字参数 参考: https ...