Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道,

这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值,

而leetcode的题,是只要找到个最大值就行,可以是[1,2]这种。

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.

Example

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

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

分析:

你给出一个整数数组(size为n),其具有以下特点:

  • 相邻位置的数字是不同的
  • A[0] < A[1] 并且 A[n - 2] > A[n - 1]

假定P是峰值的位置则满足A[P] > A[P-1]A[P] > A[P+1],返回数组中任意一个峰值的位置。

给出数组[1, 2, 1, 3, 4, 5, 7, 6]返回1, 即数值 2 所在位置, 或者6, 即数值 7 所在位置.

要找峰值,要分析每个点有哪几种情况,每个只有四种情况,

(1)在某个峰值

(2)在某个上升区间

(3)在某个下降区间

(4)在一个谷底,比左右两边的元素都小。

首先我们可以确定的是,第一个元素和最后一个元素不可能构成一个峰值,因为峰值要求某个值要同时大于左右两侧的数。

限定了start和end的范围,这道题我们用二分法解决。

第三个条件选择里 else{ start  = mid} 也可以是 end = mid。

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

lintcode 75 Find Peak Element的更多相关文章

  1. (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...

  2. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

  3. lintcode : find peak element 寻找峰值

    题目 寻找峰值 你给出一个整数数组(size为n),其具有以下特点: 相邻位置的数字是不同的 A[0] < A[1] 并且 A[n - 2] > A[n - 1] 假定P是峰值的位置则满足 ...

  4. [LintCode] Find Peak Element 求数组的峰值

    There is an integer array which has the following features: The numbers in adjacent positions are di ...

  5. Lintcode: Find Peak Element

    There is an integer array which has the following features: * The numbers in adjacent positions are ...

  6. 【Lintcode】075.Find Peak Element

    题目: There is an integer array which has the following features: The numbers in adjacent positions ar ...

  7. [LeetCode] Find Peak Element 求数组的局部峰值

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  8. LeetCode 162 Find Peak Element

    Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...

  9. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

随机推荐

  1. dotnetbar入门

    1.下载dotnetbar组件 2.工具箱引用 3.项目引用 4.开始工作 //此处Form完整的名称是System.Windows.Forms.Form,表示FrmMain窗体类是继承于System ...

  2. java编程思想-复用类总结

    今天继续读<java 编程思想>,读到了复用类一章,看到总结写的很好,现贴上来,给大家分享. 继承和组合都能从现有类型生成新类型.组合一般是将现有类型作为新类型底层实现的一部分来加以复用, ...

  3. JS左右栏目添加器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. host,nslookup,dig 命令安装

    host,nslookup,dig依赖bind包,所以先看一下系统有没有bind包 命令如下:rpm -qa |grep bind 如果没有或者版本太低请升级安装 命令是:yum install bi ...

  5. 10月16日上午MySQL数据库作业设计表解析

    作业设计表:多张表存储学生成绩及各种信息 需要从表里面体现: 关于学生的:代号 姓名 性别 年龄 班级 关于课程的:代号 名称 关于老师的:代号 姓名 关于成绩的:例如:闫超--网页--90 要能查看 ...

  6. Sqlserver2008 表分区教程

    先声明..Sql2008只有企业版才能够搞这个表分区.其他版本请自觉更改.. 哥在这里费了很长时间劲..结果还是老老实实的重装.. 表分区定义 一般情况下,我们建立数据库表时,表数据都存放在一个文件里 ...

  7. Struts2中ModelDriven的陷阱及其预防

    页面表单 <form action="updateInput.action" method="post"> <input type=" ...

  8. verilog描述表决器的两种方式简易分析

    命题:设计一个三变量表决器.真值表如下: 可以写出并简化得出公式:F=AB+BC+AC. 以下是两种算法: 第一种:仅从算法方面描述为:A.B.C的和大于1则输出结果为1,否则为0:源码如下: mod ...

  9. Task异常处理

    http://www.cnblogs.com/xray2005/archive/2011/08/24/2151459.html

  10. tinyfox for linux 独立版 fox.sh

    #!/bin/sh export LANG="zh_CN.UTF-8" # export MONO_IOMAP="all" # export MONO_EXTE ...