leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0162.cpp (415B)


      1 class Solution {
      2   public:
      3     int findPeakElement(vector<int> &nums) {
      4         int n = nums.size();
      5         if (n == 1) return 0;
      6 
      7         for (int i = 0; i < n - 1;) {
      8             if (nums[i] > nums[i + 1]) return i;
      9             i += 2;
     10             if (i >= n) break;
     11             if (nums[i - 1] > nums[i]) return i - 1;
     12         }
     13         if (nums[n - 1] > nums[n - 2]) return n - 1;
     14 
     15         return -1;
     16     }
     17 };