博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
162. Find Peak Element
阅读量:4343 次
发布时间:2019-06-07

本文共 1005 字,大约阅读时间需要 3 分钟。

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

Given an input array nums, where nums[i] ≠ nums[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 nums[-1] = nums[n] = -∞.

Example 1:

Input: nums = [1,2,3,1]Output: 2Explanation: 3 is a peak element and your function should return the index number 2.

Example 2:

Input: nums = [1,2,1,3,5,6,4]Output: 1 or 5 Explanation: Your function can return either index number 1 where the peak element is 2,              or index number 5 where the peak element is 6.
//Time: O(logn), Space: O(1)  1     public int findPeakElement(int[] nums) { 2         if (nums == null || nums.length == 0) { 3             return -1; 4         } 5          6         int start = 0; 7         int end = nums.length - 1; 8          9         while (start < end) {//因为有[1]这种特殊情况,所以要start

similar: 

转载于:https://www.cnblogs.com/jessie2009/p/9761937.html

你可能感兴趣的文章
RTMP
查看>>
求一个数的整数次方
查看>>
点云PCL中小细节
查看>>
铁路信号基础
查看>>
RobotFramework自动化2-自定义关键字
查看>>
[置顶] 【cocos2d-x入门实战】微信飞机大战之三:飞机要起飞了
查看>>
BABOK - 需求分析(Requirements Analysis)概述
查看>>
第43条:掌握GCD及操作队列的使用时机
查看>>
Windows autoKeras的下载与安装连接
查看>>
CMU Bomblab 答案
查看>>
微信支付之异步通知签名错误
查看>>
2016 - 1 -17 GCD学习总结
查看>>
linux安装php-redis扩展(转)
查看>>
Vue集成微信开发趟坑:公众号以及JSSDK相关
查看>>
技术分析淘宝的超卖宝贝
查看>>
i++和++1
查看>>
react.js
查看>>
P1313 计算系数
查看>>
NSString的长度比较方法(一)
查看>>
Azure云服务托管恶意软件
查看>>