[leetcode] #162 Find Peak Element

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

</pre><p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px">Given an input array where <code style="font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:12.6000003814697px; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">num[i] ≠ num[i+1]</code>, find a peak element and return its index.</p><p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px">The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.</p><p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px">You may imagine that <code style="font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:12.6000003814697px; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">num[-1] = num[n] = -∞</code>.</p><p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px">For example, in array <code style="font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:12.6000003814697px; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">[1, 2, 3, 1]</code>, 3 is a peak element and your function should return the index number 2.</p><p class="showspoilers" style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px"><a target=_blank target="_blank" href="https://leetcode.com/problems/find-peak-element/#" style="color:rgb(0,136,204); text-decoration:none; background:0px 0px">click to show spoilers.</a></p><p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px"><span style="font-weight:700">Credits:</span><br style="" />Special thanks to <a target=_blank target="_blank" href="https://oj.leetcode.com/discuss/user/ts" style="color:rgb(0,136,204); text-decoration:none; background:0px 0px">@ts</a> for adding this problem and creating all test cases.</p><p style="margin-top:0px; margin-bottom:10px; font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px"><span style="color: rgb(51, 51, 51);">这道题的大意是找到</span><strong><span style="color:#ff0000">一个</span></strong><span style="color:#333333">数组中的极大值,也就是其值大于左值与右值的数的序号。</span></p><p style="margin-top:0px; margin-bottom:10px; font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px"><span style="color:rgb(51,51,51)">对于数组的首尾,num[0]与num[n-1],分别视它们的左值和右值为</span><span style="background-color:rgb(255,255,255)">-<span style="font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:12.6000003814697px; line-height:30px">∞。</span></span></p><p style="margin-top:0px; margin-bottom:10px; font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px"><span style="background-color:rgb(255,255,255)"><span style="font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:12.6000003814697px; line-height:30px">原先看这道题还是median的题,觉得应该还有一定难度,但是没想到是个纸老虎,几行代码就搞定了。</span></span></p><p style="margin-top:0px; margin-bottom:10px; font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px"></p><pre name="code" class="cpp">int findPeakElement(int num[], int n) {
    
    for (int i=0;i<n-1;i++)
        if (num[i]>num[i+1])
            return i;
    return n-1;
}
把整个数组想象成一座山,需要找到第一个山峰,根据条件num[-1]=- ,一开始我们就处于上山状态,那么一直往前走,看到前方要下山了(num[i]>num[i+1]),目前所在就是山峰。

这道题不需要考虑多个极大值的情况,找到一个即可。如果需要找到所有的极大值,那么也不难,只是判断条件要多一点。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值