-
Notifications
You must be signed in to change notification settings - Fork 0
/
leet_offer_069.cpp
67 lines (61 loc) · 1.46 KB
/
leet_offer_069.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <numeric>
#include <string>
#include <cmath>
#include <functional>
#include <cmath>
#include <set>
#include <map>
#include <assert.h>
#include <stack>
#include <unordered_map>
using namespace std;
class Solution_linear {
public:
int peakIndexInMountainArray(vector<int>& arr) {
if(arr.size()<3)
-1;
for(int i=1;i<arr.size()-1;i++)
{
if(arr[i]>arr[i-1] && arr[i]>arr[i+1])
return i;
}
return -1;
}
};
class Solution {
public:
int peakIndexInMountainArray(vector<int>& arr) {
//二分查找
int l = 0;
int r = arr.size()-1;
while(l<=r)
{
int mid = (l+r)/2;
if(mid >0 && mid<arr.size()-1 && arr[mid]>arr[mid-1] && arr[mid]>arr[mid+1])
return mid;
else if(mid >0 && mid<arr.size()-1 && arr[mid]>arr[mid-1] && arr[mid]<arr[mid+1])
l = mid+1;
else if(mid >0 && mid<arr.size()-1 && arr[mid]<arr[mid-1] && arr[mid]>arr[mid+1])
r = mid-1;
else if(mid==0)
l = mid+1;
else if(mid==arr.size()-1)
r = mid-1;
}
return -1;
}
};
int main()
{
vector<int> t = {3,5,3,2,0};
Solution sol;
int x = sol.peakIndexInMountainArray(t);
return 0;
}