C++ Solution - 3ms


  • 0
    I
    int findPeakElement(vector<int>& nums) {
            int returnIndex=0;
            int n=nums.size();
            if(n==1)
                return 0;
            if(n == 2){
                if(nums[1] > nums[0])
                    return 1;
                else 
                    return 0;
            }
            for(int index=1;index<n;index++){
                if(nums[index] > nums[index-1] && nums[index] > nums[index+1])
                        return index;
                
                if(index == 1){
                    if(nums[index-1] > nums[index])
                        return 0;
                }else if(index == n-1){
                    if(nums[index] > nums[index-1])
                        return index;
                }
            }
            return returnIndex;
        }
    

Log in to reply
 

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