二分的运用:很是巧妙
class Solution {
public:
int findPeakElement(vector<int>& nums) {
if (nums.size() == 1) {
return 0;
}
int left = 0, right = nums.size() - 1;
int mid = (left + right) / 2;
// left 指向的值一定大于前一位
// right指向的值一定大于后一位
// 两者重合时就是峰值
while (left < right) {
if (nums[mid] < nums[mid + 1]) {
left = mid + 1;
} else {
right = mid;
}
mid = (left + right) / 2;
}
return right;
}
};