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] ...
随机推荐
- jquery判断多选框是否选中
if($("#xieyi").is(":checked")){ alert('选中'); }else{ alert('没有选中') }
- #数据结构-fib
/////////////////////////////////////////////////////////////////////////////// // // FileName : fic ...
- HDU 1358 简单kmp
题目大意: 找到所有的可组成连续字符串相连的位置,和循环字符串的个数 #include <cstdio> #include <cstring> #include <alg ...
- Android ScrollView与ViewPager滑动冲突
前段时间做项目碰到在ScrollView里添加ViewPager,但是发现ViewPager的左右滑动和ScrollView的滑动冲突了,解决这个问题的方法是重写ScrollView. 代码: pub ...
- 【转发】centos 7安装完后出现please make your choice from '1' ......
PS:出现以上信息,是要求你阅读或者接收协议: Initial setup of CentOS Linux 7 (core)解决步骤如下: 1,输入[1],按Enter键阅读许可协议,2,输入[2], ...
- BinaryWriter
c#里的文件操作 fileInfo dir的一大堆属性不用说 地球人都知道(什么fileName,create() delete()) ,文件系统的概念很好理解的 文件读写也好理解(硬盘到内存 然后再 ...
- 欢迎参加MVP主讲的Windows 10开发线上课程
博客地址:http://blog.csdn.net/FoxDave Windows 10 Developer Readiness - Powered by MVPs - 由微软最有价值专家(MVP)主 ...
- SharePoint 2013 开发——构建工作流开发环境
博客地址:http://blog.csdn.net/FoxDave 本篇我们来讲述一下如何搭建SharePoint 2013工作流开发环境. Windows Azure Workflow作为单独的可下 ...
- MATLAB里的正则表达式 [转]
正则表达式在处理字符串及文本时显得十分方便,在perl, python等脚本语言,以及java, .net等平台上都支援正则表达式.事实上,在MATLAB中也提供了正则表达式的支持.主要包含三个常用的 ...
- (转)Ratchet教程:创建项目
原文:http://www.w3cplus.com/mobile/how-to-create-mobile-project-width-ratchet.html Ratchet教程:创建项目 ...