Concise 5 lines C++ O(n) solution


  • 0
    class Solution {
    public:
        int findPeakElement(vector<int>& nums) {
            int l = 0, r = nums.size() - 1;
            while (l < r) 
                if (nums[l] < nums[r]) ++l;
                else --r;
            return l;
        }
    };
    

    Accepted and runs in 4-8ms, but I doubt that it may fail with extra test cases...Can someone correct me? :)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.