Java code that needs "break"


  • 0
    S
    public class Solution {
    public int findPeakElement(int[] num) {
        int result = 0;
        if (num.length < 2) {result = 0;}
        else if (num.length < 3) {
            if (num[0] > num[1]) {result = 0;}
            else {result = 1;}
            
        }
        else {
            for (int i = 0; i < num.length; i++) {
                if(i == 0 && num[i] > num[i+1]) {result = 0; break;}
                if(i == num.length - 1 && num[i] > num[i-1]) {result = num.length-1; break; }
                else if(num[i] > num[i+1] && num[i] > num[i-1]) {result = i; break;}
            }  
        }
        return result;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.