162. Find Peak Element
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.
代码如下:
public class Solution {
public int findPeakElement(int[] nums) {
if(nums.length==1)
return 0;
if(nums.length==2&&nums[0]<nums[1])
return 1;
for(int i=1;i<nums.length-1;i++)
{
if(nums[i]>nums[i-1]&&nums[i]>nums[i+1])
return i;
}
if(nums[nums.length-1]>nums[nums.length-2])
return nums.length-1;
return 0;
}
}
162. Find Peak Element的更多相关文章
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
- LeetCode 162 Find Peak Element
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
- 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] ≠ ...
- ✡ 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] ≠ ...
- LeetCode OJ 162. Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode 162. Find Peak Element (找到峰值)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- (二分查找 拓展) 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 ...
- 162. Find Peak Element (Array; Divide-and-Conquer)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- 162. Find Peak Element(二分查找 )
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ...
随机推荐
- NSString asscii格式(2进制) 转 utf8格式——解决iOS自己处理http socket数据,遇到Transfer-Encoding: chunked时
因为需要实现自己的http客户端,就要自己模拟http 的socket通讯: 上行不难,自己处理好http即可. 但下行时,服务器端的动态语言返回数据有可能会是这种格式: http头 16进制表示的数 ...
- win10系统的点评
Windows 10 是美国微软公司所研发的新一代跨平台及设备应用的操作系统.在正式版本发布一年内,所有符合条件的Windows7.Windows 8.1的用户都将可以免费升级到Windows 10, ...
- Fedora20的一些个人配置
0,老传统 yum install screenfetch 1,关闭蜂鸣器 edit /etc/bashrc setterm -blength 0#setterm -bfreq 10 #这个可以设置声 ...
- 安装eclipse for c/c++环境
安装eclipse for c/c++环境: 1.启动eclipse, 2.选择Help->Install New Software...,在Work with的框框下复 ...
- switch… case 语句的用法(一)
public class Test7 { public static void main(String[] args) { int i=5; switch(i) { case 1: System.ou ...
- ODI 12.1.3创建standalone代理
首先要安装ODI. ODI安装 如果没有安装WLS,则可以选择独立安装,如下图.
- T-sql语句
在用代码编辑数据库时,首先要启动WAMPSERVER 1.创建表 create table Car ( Code varchar(50) primary key , Name varchar(50) ...
- <二叉树的基本操作(有层次遍历)>
#include<stdio.h> #include<stdlib.h> #include<string.h> #define num 100 #define OK ...
- JSON解析和XML解析
一. XML:用到一个开源解析类,GDataXMLNode(将其加入项目中),添加libxml2.dylib框架 经常用到的方法: 1.- (id)initWithXMLString:(NSStrin ...
- IOS的变量前加extern和static字段
IOS的变量前加extern和static字段 前一阵子,做项目的时候到网上找Demo,打开运行的时候发现其中变量前有关键字extern和static,所以我研究了一下子 对于extern来说可以理解 ...