Java- My simple solution


  • -5
    R

    My simple solution

     public class Solution {
            public int findPeakElement(int[] num) {
                if(num.length == 0){
                    return 0;
                }
                
                int[] temp = new int[num.length];
                for(int i = 0; i < num.length; i++){
                    temp[i] = num[i];
                }
                
                Arrays.sort(temp);
                int max = temp[temp.length -1];
                int count = 0;
                for(int i = 0; i < num.length; i++){
                    if(num[i] == max){
                        count = i;
                        break;
                    }
                }
                
                return count;
            }
        }

  • 2
    L

    too simple and useless, and not the logarithmic complexity.


  • 0
    J

    a little complex,what's the runtime?


  • 0
    Y

    your piece of code requires extra space for a temporary array and the runtime is O(nLog(n)). even a simple one by one search will be way better than this


Log in to reply
 

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