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)

解法一:

 1 class Solution {
 2 public:
 3     /*
 4      * @param A: An integers array.
 5      * @return: return any of peek positions.
 6      */
 7     int findPeak(vector<int> &A) {
 8         if (A.empty()) {
 9             return -1;
10         }
11
12         int start = 0;
13         int end = A.size() - 1;
14
15         while (start + 1 < end) {
16             int mid = start + (end - start) / 2;
17
18             if (A[mid] > A[mid - 1]) {
19                 if (A[mid] > A[mid + 1]) {
20                     return mid;
21                 }
22                 else {
23                     start = mid;
24                 }
25             }
26             else {
27                 if (A[mid] > A[mid + 1]) {
28                     end = mid;
29                 }
30                 else {
31                     start = mid;
32                 }
33             }
34         }
35
36         return -1;
37     }
38 };

分类讨论。

解法二:

 1 class Solution {
 2     /**
 3      * @param A: An integers array.
 4      * @return: return any of peek positions.
 5      */
 6     public int findPeak(int[] A) {
 7         // write your code here
 8         int start = 1, end = A.length-2; // 1.答案在之间,2.不会出界
 9         while(start + 1 <  end) {
10             int mid = (start + end) / 2;
11             if(A[mid] < A[mid - 1]) {
12                 end = mid;
13             } else if(A[mid] < A[mid + 1]) {
14                 start = mid;
15             } else {
16                 end = mid;
17             }
18         }
19         if(A[start] < A[end]) {
20             return end;
21         } else {
22             return start;
23         }
24     }
25 }

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

转载于:https://www.cnblogs.com/abc-begin/p/7551585.html

75. Find Peak Element 【medium】相关推荐

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

  2. 114. Flatten Binary Tree to Linked List【Medium】【将给定的二叉树转化为“只有右孩子节点”的链表(树)】...

    Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1/ ...

  3. 社会生存的75条忠告----胜读十年书【转】

    01.犯了错误就该诚实地认错--狡辩.诿过只会害了自己. 02.朋友之间要保持距离--这样的友谊才能长久. 03.钱追人,人追健康--有了健康,还怕挣不到钱么? 04.别轻易转行--转行的风险很高,最 ...

  4. 【Leetcode】【Medium】Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  5. 【medium】220. Contains Duplicate III

    因为要考虑超时问题,所以虽然简单的for循环也可以做,但是要用map等内部红黑树实现的容器. Given an array of integers, find out whether there ar ...

  6. 310. Minimum Height Trees 【Medium】 树

    题目:给出一个树,求出所有作为根节点时树的高度最小的节点 思路:用剪枝的办法,每次剪掉叶子节点,最终剩下一个或者两个节点时结束. 原理:一个节点的树的高度为该节点到最远的节点的距离,且此最远节点必为叶 ...

  7. LeetCode | 0077. Combinations组合【Python】

    LeetCode 0077. Combinations组合[Medium][Python][回溯] Problem LeetCode Given two integers n and k, retur ...

  8. LeetCode | 0017. Letter Combinations of a Phone Number电话号码的字母组合【Python】

    LeetCode 0017. Letter Combinations of a Phone Number电话号码的字母组合[Medium][Python][回溯][DFS][暴力] Problem L ...

  9. LeetCode | 0365. Water and Jug Problem水壶问题【Python】

    LeetCode 0365. Water and Jug Problem水壶问题[Medium][Python][BFS][数学] Problem LeetCode You are given two ...

最新文章

  1. 伍六七带你学算法 进阶篇-排序算法
  2. aop对请求后端的参数修改_Spring Boot AOP之对请求的参数入参与返回结果进行拦截处理...
  3. Mongodb常用增删改查语法
  4. java模拟退火程序
  5. win10一直卡在自动修复_Win10今年最重磅更新!低CPU占用率+16大新特性,系统快如闪电!...
  6. potential things for recommendation
  7. JavaScript事件冒泡
  8. oracle强制切换redolog组
  9. php 非递归调用,php 无限分类(非递归)
  10. Orcle 版本、数据库名查询
  11. CV中必要的数学知识_奇异值的物理意义是什么?
  12. 菜鸟教程网oracle,Oracle数据库入门教程 Oracle数据库菜鸟教程
  13. 基于Springboot+Mybatisplus的学校学院门户学生就业指导管理系统
  14. 联想M7650DF加粉和重置/清零的正确方法
  15. TA505武器之隐形电子邮件窃取器
  16. 手写解析微信Matrix性能监控日志的工具
  17. NamedParameterJdbcTemplate传参的n种写法
  18. 《产品经理深入浅出》PART 3:产品经理专业技能
  19. UVA - 1389 Hard Life【分数规划+最小割】【最大权闭合图】
  20. 2023最新SSM计算机毕业设计选题大全(附源码+LW)之java影视资源分享论坛23562

热门文章

  1. java card applet_可多选的javacard applet | 学步园
  2. python数据分析第七章实训3_《利用python进行数据分析》读书笔记--第七章 数据规整化:清理、转换、合并、重塑(二)...
  3. android控件触摸缩放,Android控件之ZoomControls缩放使用
  4. 3种mysql的储存机制_MySQL三种InnoDB、MyISAM和MEMORY存储引擎对比
  5. 解压ubi文件_制作ubi文件系统
  6. javascript的Math对象和全局函数
  7. 使用OpenCV和Python高效计算视频的总帧数
  8. 【PCL】的五大依赖库及作用
  9. HDU 2586 How far away ? LCA ---tanjar+并查集 离线算法
  10. websphere mq 查看队列中是否有数据_全网最全的 “消息队列”