LeetCode-Solutions-in-Cpp17

Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.


Project maintained by downdemo Hosted on GitHub Pages — Theme by mattgraham
class Solution {
 public:
  int findPeakElement(vector<int>& nums) {
    auto it = adjacent_find(nums.begin(), nums.end(), greater<int>{});
    return it == nums.end() ? nums.size() - 1 : it - nums.begin();
  }
};
class Solution {
 public:
  int findPeakElement(vector<int>& nums) {
    int l = 0;
    int r = nums.size();
    while (l < r) {
      int m = l + (r - l) / 2;
      if (m > 0 && nums[m] < nums[m - 1]) {
        r = m;
      } else {
        l = m + 1;
      }
    }
    return l - 1;
  }
};