题目:

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. LeetCode No.109,110,111

    No.109 SortedListToBST 有序链表转换二叉搜索树 题目 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的 ...

  2. android 9.0 http无法访问问题

    在res/xml下新建network-security-config.xml <?xml version="1.0" encoding="utf-8"?& ...

  3. Introduction to Differential Equations,Michael E.Taylor,Page 3,4 注记

    此文是对 [Introduction to Differential Equations,Michael E.Taylor] 第3页的一个注记.在该页中,作者给了微分方程$$\frac{dx}{dt} ...

  4. [LC] 96. Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...

  5. OpenCL介绍

    OpenCL(全称Open Computing Language,开放运算语言)是第一个面向异构系统通用目的并行编程的开放式.免费标准,也是一个统一的编程环境,便于软件开发人员为高性能计算服务器.桌面 ...

  6. OpenCV 特征点检测

    #include <stdio.h> #include <iostream> #include "opencv2/core/core.hpp" #inclu ...

  7. mongodb 前人埋坑的奇怪问题

    接手一个很老的项目 数据库用的mongodb 代码里的collections表名用的auth没问题 直接去monogdb show table 确实有auth表 想在本地登录,看看用户信息 > ...

  8. 系统学习javaweb1----HTML语言1

    自我感受:HTML语言没想到也有这么大的学问,竟然能通过超链接标签直接访问百度,这可让我大吃一惊,我也得反思一下自己,上学期的java纯是混过来的,没有系统的学习过,感觉能通过期末考试都是侥幸,接下来 ...

  9. Tomcat源码解析-启动过程分析之主干流程

    Tomcat启动入口就在脚本startup.sh中,具体脚本可以看tomcat的源码,这个启动脚本主要用来判断环境,找到catalina.sh脚本路径,将启动参数传递给catalina.sh执行.ca ...

  10. bootstrap 和datapicker 样式不兼容修复

    修改 datepicker.js内的 layout 方法 function(el) { var options = $(el).data('datepicker'); var cal = $('#' ...