zoukankan      html  css  js  c++  java
  • Find Peak Element

    A peak element is an element that is greater than its neighbors.

    Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.

    The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.

    You may imagine that num[-1] = num[n] = -∞.

    For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.

    click to show spoilers.

     

    Runtime: 8ms

     1 class Solution {
     2 public:
     3     int findPeakElement(vector<int>& nums) {
     4         if(nums.empty()) return 0;
     5         if(nums.size() == 1) return 0;
     6         if(nums[0] > nums[1]) return 0;
     7        
     8         int n = nums.size();
     9         
    10         for(int i = 1; i < n - 1; i++){
    11             if(nums[i] > nums[i - 1] && nums[i] > nums[i + 1])
    12                 return i;
    13         }
    14         if(nums[n - 1] > nums[n - 2]) return n - 1;
    15     }
    16 };
  • 相关阅读:
    VM player无法联网问题
    寄存器
    linux下的文件操作
    linux的切换目录操作
    linux的ls -al指令
    python对ASC码的加减
    ASC码速记
    pyhton的返回值
    intellij 调试方法
    2015,5.10
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/4855289.html
Copyright © 2011-2022 走看看