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. ZOJ 3949 Edge to the Root(树形DP)

    [题目链接] http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3949 [题目大意] 给出一棵根为1的树,每条边边长为1,请你 ...

  2. [Contest20180314]数列

    数据范围告诉我们要写两档的分 第一档:$M\leq200,N\leq10^9$,可以枚举$m$计算答案 直接矩阵快速幂:$O\left(M^4\log_2N\right)$,会超时,所以我们需要某些“ ...

  3. Spring Boot中使用MyBatis注解配置详解

    传参方式 下面通过几种不同传参方式来实现前文中实现的插入操作. 使用@Param 在之前的整合示例中我们已经使用了这种最简单的传参方式,如下: @Insert("INSERT INTO US ...

  4. python 实现汉诺塔问题

    代码如下: def hano(n,x,y,z): if n==1: print(x,"->",z) else: #将n-1个盘子从x->y hano(n-1,x,z,y ...

  5. iOS 10 资料整理笔记

    1.Notification(通知) 自从Notification被引入之后,苹果就不断的更新优化,但这些更新优化只是小打小闹,直至现在iOS 10开始真正的进行大改重构,这让开发者也体会到UserN ...

  6. Git学习笔记(一) 安装及版本库介绍

    安装Git 最早Git是在Linux上开发的,很长一段时间内,Git也只能在Linux和Unix系统上跑.不过,慢慢地有人把它移植到了Windows上.现在,Git可以在Linux.Unix.Mac和 ...

  7. MythXinWCF通用宿主绿色版V1.1

    更新内容:宿主的唯一编号和名称可以输入符号"."日志文本框增加滚动条,并且总是显示文本末端增加启动方式选择:1.手动启动 2.跟随系统启动 最新下载地址: http://pan.b ...

  8. 利用LogParser分析IIS日志

    LogParser是微软官方出品的用于读取分析IIS日志的工具,使用类SQL语句过滤文本日志内容,并可将内容导出到csv.sqlserver作进一步分析    下载地址:http://www.micr ...

  9. oracle 11g 大量废连接占满数据库连接问题处理

    问题描述: 数据库不断出现大量无用连接,超过数据库最大连接数,导致新的连接无法建立,访问不通数据库 问题分析: 服务器netstat连接数,大量连接来自办公网连接,不断在增加,通过服务器spid查看数 ...

  10. Web服务器在外网能裸奔多久?

      很多时候我们轻易地把Web服务器暴露在公网上,查看一下访问日志,可以看到会收到大量的攻击请求,这个是网站开通后几个小时收到的请求: 1.  探测服务器信息 在上线一分钟,收到OPTION请求探测. ...