LeetCode OJ: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.
感觉不太像medium的题目,但是我写的比价乱啊,代码如下:
class Solution {
public:
int findPeakElement(vector<int>& nums) {
if(nums.size() == || nums.size() == )
return ;
if(nums[] > nums[])
return ;
for(int i = ; i < nums.size(); ++i){
if(nums[i] > nums[i - ]){
if(i + >= nums.size())
return i;
else if(nums[i] > nums[i + ])
return i;
else ;
}
}
}
};
java代码:
public class Solution {
public int findPeakElement(int[] nums) {
if(nums.length == || nums.length == )
return ;
if(nums[] > nums[])
return ;
for(int i = ; i < nums.length; ++i){
if(nums[i] > nums[i - ]){
if(i+ >= nums.length || nums[i] > nums[i+])
return i;
}
}
return nums.length - ;
}
}
LeetCode OJ:Find Peak Element(寻找峰值元素)的更多相关文章
- [LeetCode] 162. Find Peak Element 查找峰值元素
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- 162 Find Peak Element 寻找峰值
峰值元素是指其值大于左右相邻值的元素.给定一个输入数组,其中 num[i] ≠ num[i+1],找到峰值元素并返回其索引.数组可能包含多个峰值,在这种情况下,返回到任何一个峰值所在位置都可以.你可以 ...
- lintcode : find peak element 寻找峰值
题目 寻找峰值 你给出一个整数数组(size为n),其具有以下特点: 相邻位置的数字是不同的 A[0] < A[1] 并且 A[n - 2] > A[n - 1] 假定P是峰值的位置则满足 ...
- Leetcode162. Find Peak Element寻找峰值
示例 2: 输入: nums = [1,2,1,3,5,6,4] 输出: 1 或 5 解释: 你的函数可以返回索引 1,其峰值元素为 2: 或者返回索引 5, 其峰值元素为 6. 说明: 你的解法 ...
- Leetcode之二分法专题-162. 寻找峰值(Find Peak Element)
Leetcode之二分法专题-162. 寻找峰值(Find Peak Element) 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+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] ≠ ...
- leetcode 【 Find Peak Element 】python 实现
题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...
- ✡ 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】Find Peak Element ☆
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
随机推荐
- Java集合类学习记录
被标记为transient的属性在对象被序列化的时候不会被保存int[] arr1 = {1, 2, 3, 4, 5}; int[] arr2 = Arrays.copyOf(arr1, new_le ...
- 五分钟学会 Kotlin 语法
为什么使用Kotlin 项目一期在收尾了终于有时间折腾了,一个多月以来Kotlin从入门到现在,坚持用来开发的切身感受.因为语法与Java的区别挺大的一开始很想放弃,如果不是因为项目在使用,想必很少人 ...
- Python-Cpython解释器支持的进程与线程
一.Python并发编程之多进程 1. multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在pyt ...
- 配置数据库,Flask-Alchemy
Flask-Alchemy连接数据库的插件 获取当前项目路径(绝对路径) 来自为知笔记(Wiz)
- image_Magic
http://www.charry.org/docs/linux/ImageMagick/ImageMagick.html mogrify -sample 25% *.jpg 批量处理图片 conv ...
- s5_day14作业
import re # 1. 匹配一段文本中的每行的邮箱 # ret=re.findall('\w+@\w+\.com','10000@qq.com,qwe48645313@163.com') # p ...
- PL/SQL编程—变量
SQL> declare c_tax_rate ,):=0.03; v_name ); v_passwd ); v_sale ,); v_tax_sale ,); begin select na ...
- 『NiFi 学习之路』入门 —— 下载、安装与简单使用
一.概述 "光说不练假把式." 官网上的介绍多少让人迷迷糊糊的,各种高大上的词语仿佛让 NiFi 离我们越来越远. 实践是最好的老师.那就让我们试用一下 NiFi 吧! 二.安装 ...
- application/x-www-form-urlencoded和multipart/form-data
我们在提交表单的时候,form表单参数中会有一个enctype的参数. EncType表明提交数据的格式,用 Enctype 属性指定将数据发到服务器时浏览器使用的编码类型. enctype指定了H ...
- 【Java Web】新手教程(转)
转自:http://www.journaldev.com/1854/java-web-application-tutorial-for-beginners#web-server-client Web ...