leetcode_162题——Find Peak Element(数组,水题)

Find Peak Element

Total Accepted: 24838 Total Submissions: 79757My Submissions

Question Solution

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.

click to show spoilers.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

Hide Tags

Array Binary Search

Have you met this question in a real interview?

Yes

No

Discuss


C++
Java
Python
C
C#
JavaScript
Ruby

这题如果只求做出来的话比较水,没啥好说的

#include<iostream>
#include<vector>
using namespace std;
int findPeakElement(vector<int>& nums) {
	int len=nums.size();
	if(len==1)
		return 0;
	if(len==2)
		nums[0]>nums[1]?0:1;
	if(nums[0]>nums[1])
		return 0;
	if(nums[len-1]>nums[len-2])
		return len-1;

	for(int i=1;i<len-1;i++)
	{
		if(nums[i]>nums[i-1]&&nums[i]>nums[i+1])
			return i;
	}
}

int main()
{

}

  

时间: 2024-10-06 01:14:58

leetcode_162题——Find Peak Element(数组,水题)的相关文章

HDU1166 敌兵布阵 树状数组水题

中文题目,很简单的题目,区间求和,当然对于线段树来说也很水,为了练习一下树状数组,多做做水题吧,加深理解,并且打好基础,我算是被没打好基础给吓坏了,宁可多花几个小时 刷刷水题扎实点, 很裸的题目 操作也很裸,了解树状数组的肯定能做 #include<iostream> #include<cstdio> #include<list> #include<algorithm> #include<cstring> #include<string&g

Lightoj 1112 - Curious Robin Hood 【单点改动 + 单点、 区间查询】【树状数组 水题】

1112 - Curious Robin Hood PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick.

Lightoj 1112 - Curious Robin Hood 【单点修改 + 单点、 区间查询】【树状数组 水题】

1112 - Curious Robin Hood PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick.

【不可能的任务1/200】bzoj1103树状数组水题

(卧槽,居然规定了修改的两点直接相连,亏我想半天) 非常水的题,用dfs序(而且不用重复,应该是直接规模为n的dfs序)+树状数组可以轻松水 收获:树状数组一遍A(没啥好骄傲的,那么简单的东西) 1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 int N=0,n,m,p,q,a[300000],l[300000],pos[300000],end[300000],son[300000],bro[30

hdu1541 Stars 树状数组水题

Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5378    Accepted Submission(s): 2125 Problem Description Astronomers often examine star maps where stars are represented by points on a pla

HDU 5147 Sequence II 树状数组水题

无聊咯,学某y,水一发 给你n个数的排列,A[1]到A[n],统计多少四元组(a,b,c,d)满足,1<=a<b<c<d<=n,且A[a]<A[b],A[c]<A[d] 树状数组统计前缀和咯 1 #include<cstdio> 2 #include<cstring> 3 #include<cctype> 4 typedef long long ll; 5 const int maxn=5e4+10; 6 int T,n,a[m

hdu 1541 Stars 树状数组水题

Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5210    Accepted Submission(s): 2053 Problem Description Astronomers often examine star maps where stars are represented by points on a pla

hdu 2838 树状数组水题

提议是给你一个序列   让你调整把它变成 从小到大排列的有序序列    没调动两个为两权值之和   问最小的权值和是多少 给个数列   1 4 2 3 5      对每一个位置数   需要交换的比为前面比它大的数  或后面比它小的数(包含了最小值在里面了)     比如pi前面有5个数比它大   则就需要把这5个数和pi交换   交换的权值就是这5个数的和再加上5*pi      庵后对每个数都这样求就为最后结果      我是把它倒过来然后按求逆序数的方式 求     有两个更新和查询 co

Distinct Substrings SPOJ - DISUBSTR(后缀数组水题)

求不重复的子串个数 用所有的减去height就好了 推出来的... #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include