-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem_0162_findPeakElement.cc
65 lines (60 loc) · 1.13 KB
/
Problem_0162_findPeakElement.cc
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
int findPeakElement(vector<int> &nums)
{
int n = nums.size();
if (n < 2)
{
return 0;
}
if (nums[0] > nums[1])
{
return 0;
}
if (nums[n - 1] > nums[n - 2])
{
return n - 1;
}
int l = 1;
int r = n - 2;
int m = 0;
// 为什么二分查找大的那一半一定会有峰值呢?
// 很简单,上坡必有坡顶,就算一直递增到最后一个元素,也满足条件,因为nums[-1] = nums[n] = -∞
while (l <= r)
{
m = (r - l) / 2 + l;
if (nums[m - 1] < nums[m] && nums[m] > nums[m + 1])
{
return m;
}
else if (nums[m - 1] > nums[m])
{
r = m - 1;
}
else
{
l = m + 1;
}
}
return l;
}
};
void testFindPeakElement()
{
Solution s;
vector<int> n1 = {1, 2, 3, 1};
vector<int> n2 = {1, 2, 1, 3, 5, 6, 4};
EXPECT_EQ_INT(2, s.findPeakElement(n1));
EXPECT_EQ_INT(5, s.findPeakElement(n2));
EXPECT_SUMMARY;
}
int main()
{
testFindPeakElement();
return 0;
}