-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_peak_element.cpp
49 lines (46 loc) · 1.13 KB
/
find_peak_element.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// =====================================================================================
//
// Filename: find_peak_element.cpp
//
// Description: 162. Find Peak Element.
//
// Version: 1.0
// Created: 09/12/2019 09:59:46 AM
// Revision: none
// Compiler: g++
//
// Author: Zhu Xianfeng (), [email protected]
// Organization:
//
// =====================================================================================
#include <stdio.h>
#include <vector>
class Solution
{
public:
int findPeakElement(std::vector<int>& nums)
{
int left = 0;
int right = nums.size() - 1;
while (left < right)
{
int mid = (left + right) / 2;
if (nums[mid] > nums[mid + 1])
{
right = mid;
}
else
{
left = mid + 1;
}
}
return left;
}
};
int main(int argc, char* argv[])
{
std::vector<int> nums = {1, 2, 1, 3, 5, 6, 4};
int idx = Solution().findPeakElement(nums);
printf("Index of one peak element: %d\n", idx);
return 0;
}