Leetcode 852. Peak Index in a Mountain Array

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Peak Index in a Mountain Array

2. Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
int peakIndexInMountainArray(vector<int>& A) {
int left = 0;
int right = A.size() - 1;
while(left < right) {
int mid = (left + right) / 2;
if(A[mid] > A[mid + 1]) {
right = mid;
}
else {
left = mid + 1;
}
}
return left;
}
};

Reference

  1. https://leetcode.com/problems/peak-index-in-a-mountain-array/description/
如果有收获,可以请我喝杯咖啡!