7-line JAVA solution


  • -4
    L
    Note that we just need to identify where decreasing happens.
    
    
    
       public class Solution {
                public int findMin(int[] num) {
                    if(num.length==1)
                        return num[0];
                    else{
                        for(int i=1;i<num.length;i++){
                            if(num[i]<num[i-1])
                                return num[i];
                        }
                        return num[0];
                    }
                }
            }

  • -5
    L
      for (int i = 0; i < num.length - 1; i++) {
                if (num[i] > num[i + 1]) {
                    return num[i + 1];
                }
            }
      return num[0];
    

    This is enough.


  • 0
    I

    this is bad
    O(n)


Log in to reply
 

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