#include
#include
#include
#include
using namespace std;
int FindPeakElement(vector vec)
{
vec.insert(vec.begin(), INT_MIN);
vec.push_back(INT_MIN);
for (int i = 1; i < vec.size() - 1; i++)
{
if (vec[i] > vec[i - 1] && vec[i] > vec[i + 1])
{
return (i - 1);
}
}
return -1;
}
int main()
{
srand((unsigned)time(NULL));
vector vecint;
for (int i = 0; i < 10; i++)
{
int val = rand() % 100;
cout << val << " ";
vecint.push_back(val);
}
cout << endl;
cout << FindPeakElement(vecint) << endl;
system(“pause”);
return 0;
}
Find Peak Element 求数组的局部峰值
最新推荐文章于 2024-10-31 13:57:11 发布