leetcode 162 —— Find Peak Element

来源:互联网 发布:彩票k线图软件 编辑:程序博客网 时间:2024/05/17 07:35

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.


思路:用栈保存递增序列


class Solution {public:int findPeakElement(vector<int>& nums) {stack<int> stk;stk.push(0);int i = 1;for (i; i < nums.size(); i++) {if (nums[i] >= nums[i - 1])stk.push(i);elsereturn stk.top();}return stk.top();}};



0 0