LeetCode 题解之Find Peak Element

1、题目描述

2、题目分析

在数组的首尾各加入INT_MIN ,然后遍历数组。

 

3、代码

 1 int findPeakElement(vector<int>& nums) {
 2         if( nums.size() == 1 )
 3             return 0;
 4         
 5         nums.insert( nums.begin(), INT_MIN );
 6         nums.push_back( INT_MIN );
 7         
 8         
 9         for( vector<int>::iterator it = nums.begin()+1; it != nums.end() - 1; ++it ){
10             if( *it > *(it + 1) && *it > *(it-1) )
11                 return (it - nums.begin() - 1);
12         }
13     }

 

posted @ 2018-07-12 14:12  山里的小勇子  阅读(161)  评论(0编辑  收藏  举报