题目:

A peak element is an element that is greater than its neighbors.

Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.

The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.

You may imagine that num[-1] = num[n] = -∞.

For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.

Note:

Your solution should be in logarithmic complexity.

思路:

1.查找,时间复杂度O(logn)使用二分法。

2.在如下图所示情况下,有极大值:

3.长度为1时,由于nums[-1]和nums[n]为负无穷,故0即为极大值下标。

4.根据nums[mid]不同情况分析:

(1)nums[mid]比两边数大,mid即为极大值下标。

(2)nums[mid]比左边大,比右边小,极大值可能在mid右侧,故start= mid。

(3)nums[mid]比右边大,比左边小,极大值可能在mid左侧,故end=mid。

(4)nums[mid]比两边都小,根据nums[end]与nums[end-1]判断。

代码:

 public class Solution {
public int findPeakElement(int[] nums) {
int start = 0, end = nums.length-1,mid = 0;
if(nums.length == 1){
return 0;
}
while(start + 1 < end){
mid = start + (end - start)/2;
if(nums[mid] > nums[mid-1] && nums[mid] > nums[mid+1]){
return mid;
}else if(nums[mid] < nums[mid+1] && nums[mid] > nums[mid-1]){
start = mid;
}else if(nums[mid] > nums[mid+1] && nums[mid] < nums[mid-1]){
end = mid;
}else if(nums[end-1] > nums[end]){
start = mid;
}else{
end = mid;
}
}
if(nums[start] > nums[end]){
return start;
}
return end;
}
}

LeetCode 162.Find Peak Element(M)(P)的更多相关文章

  1. LeetCode 162. Find Peak Element (找到峰值)

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

  2. (二分查找 拓展) 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 ...

  3. [LeetCode] 162. Find Peak Element 查找峰值元素

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

  4. LeetCode 162 Find Peak Element

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

  5. Java for LeetCode 162 Find Peak Element

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

  6. ✡ leetcode 162. Find Peak Element --------- java

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

  7. leetcode 162 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

    一.题目链接: https://leetcode.com/problems/find-peak-element/ 二.题目大意: 给定一个长度为N的一维数组,数组是无序的,要求找到数组中的极大值(或局 ...

  9. 【LeetCode】162. Find Peak Element 解题报告(Python)

    [LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...

随机推荐

  1. thinking in java学习笔记:14章 类型信息

    14.2 Class 对象 https://github.com/zhaojiatao/javase 1.什么是Class对象,Class对象是用来做什么的? Class对象是java程序用来创建类的 ...

  2. [LC] 161. One Edit Distance

    Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...

  3. 实例理解scala 隐式转换(隐式值,隐式方法,隐式类)

    作用 简单说,隐式转换就是:当Scala编译器进行类型匹配时,如果找不到合适的候选,那么隐式转化提供了另外一种途径来告诉编译器如何将当前的类型转换成预期类型.话不多说,直接测试 ImplicitHel ...

  4. react 踩坑记

    yarn  node-sass 安装失败 yarn config set sass-binary-site http://npm.taobao.org/mirrors/node-sass yarn i ...

  5. 一句话看懂redux

    一个是存储在 store 里面的 state,另一个是每一次调用 dispatch 所传进来的 action.reducer 的作用,就是对 dispatch 传进来的 action 进行处理,并将结 ...

  6. Python---4字符串与编码

    字符编码 字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte),所以,一个 ...

  7. 查漏补缺:QT入门

    1.什么世QT Qt是一个跨平台的C++图形用户界面应用程序框架,为应用程序开发者提供建立艺术级图形界面所需的所有功能.它是完全面向对象的,容易扩展,并且允许真正的组建编程. 2.支持平台 Windo ...

  8. Hexo搭建总结

    Hexo搭建过程记录 1.Hexo基本环境搭建 1.Hexo安装前提 Node.js和Git,他们的安装方法可以自行百度. 2.具体安装步骤可以参考: https://www.cnblogs.com/ ...

  9. 为什么 generator 忽略第一次 next 调用的参数值呢?

    首先要理解几个基本概念. 执行生成器不会执行生成器函数体的代码,只是获得一个遍历器 一旦调用 next,函数体就开始执行,一旦遇到 yield 就返回执行结果,暂停执行 第二次 next 的参数会作为 ...

  10. 阿里云https免费证书配置-包教会

      阿里云https免费证书配置-包教会-有需要请联系小编! 小编个人站点:https://www.itdog.site/ 小编微信号:wvqusrtg