Leetcode Find Peak Element

Source: Internet
Author: User

A peak element is an element, which is greater than its neighbors.

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

May imagine 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.

1  Public classSolution {2      Public intFindpeakelement (int[] num) {3         intPeak =Integer.min_value;4         intres = 0;5          for(inti = 1; i < num.length-1; i++) {6             if(Num[i] > Num[i-1] && num[i] > num[i + 1]&&num[i]>Peak) {7res =i;8Peak =Num[i];9             }Ten         } One         if(Num[0] > Num[res] && num[0] > Num[num.length-1]) { A             return0; -}Else if(Num[num.length-1] > Num[0] && num[num.length-1] >Num[res]) { -             returnNum.length-1; the}Else returnRes; -     } -}

Leetcode Find Peak Element

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.