Wednesday, June 21, 2017

162. Find Peak Element (More concise solution)

 public int findPeakElement(int[] nums) {  
     if(nums==null || nums.length<2) return 0;  
     int l=0;  
     int r=nums.length-1;  
     while(l<r){  
       int m=(r-l)/2+l;  
       if(nums[m]>nums[m+1]){  
        r=m;   
       }  
       else{  
         l=m+1;  
       }  
     }  
     return l;  
   }  

No comments:

Post a Comment