JAVA Solution


  • 0
    W
    public static int findMin(int[] nums) {
            if (nums.length == 0) {
                return 0;
            }
            if (nums.length == 1) {
                return nums[0];
            }
            if (nums.length == 2) {
                return min(nums[0], nums[1]);
            }
            if (nums[0] < nums[nums.length - 1]) {
                return nums[0];
            }
    
            if (nums[0] >= nums[nums.length - 1]) {
                int i = nums.length - 1;
                while ((i - 1) >= 0) {
                    if (nums[i - 1] > nums[i]) {
                        return nums[i];
                    }
                    i--;
                }
            }
            return nums[0];
        }
    
    private static int min(int a, int b) {
            if (a > b) {
                return b;
            }
    
            return a;
        }
    

Log in to reply
 

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