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. [CF911G]Mass Change Queries

    题目大意: 给你一个长度为n的数列a,按顺序进行以下m次操作,每次将区间[l,r]中的所有x变成y,问最后数列是怎样的. 思路: 线段树. 每个线段树结点上维护当前区间每个数分别会变成多少.时间复杂度 ...

  2. jeeplus中两个项目redis冲突问题

    修改端口号[两个项目使用不同的database]

  3. 从cmd连接mysql数据库控制台

    在cmd中进入mysql安装目录的bin目录然后执行命令 mysql -uuser -ppassword database比如用户名为root,密码为mysql,数据库为test命令如下mysql - ...

  4. pythonGUI菜单栏和弹出菜单

    菜单栏代码: from tkinter import * root = Tk() menubar = Menu(root) def callback(): pass filemenu = Menu(m ...

  5. WPF 中依赖属性的继承(Inherits)

    WPF中依赖属性的值是是可以设置为可继承(Inherits)的,这种模式下,父节点的依赖属性会将其值传递给子节点.例如,数据绑定中经常使用的DataContextProperty: var host ...

  6. Ubuntu 14 中 VirtualBox发生错误Kernel driver not installed (rc=-1908)

    宿主系统是Ubuntu 14,在VirtualBox中安装 CentOS 6.5 时,提示如下错误: Kernel driver not installed (rc=-1908) 网友提供的解决方案: ...

  7. c# 播放mp3

    转载:http://www.cnblogs.com/igrl/archive/2010/03/29/1699975.html /// <summary> /// 播放MP3文件 /// & ...

  8. QEMU, a Fast and Portable Dynamic Translator

    AbstractWe present the internals of QEMU, a fast machine emulator using an original portable dynamic ...

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

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

  10. angular get/post 下载 excel

    阅读目录 get请求 post请求 最近做项目,就碰到一个导出excel表格的功能.原本是想利用web前台导出excel的,但是最后因为两点放弃了,第一点,因为中文乱码,第二点,有分页(在前台导出ex ...