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(begin(nums), end(nums), greater<int>{});
    return it == end(nums) ? size(nums) - 1 : it - begin(nums);
  }
};
class Solution {
 public:
  int findPeakElement(vector<int>& nums) {
    int l = 0;
    int r = size(nums);
    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;
  }
};