Template for both #153 #154


  • 0

    I think it is confusing to someone like myself when I saw those posts comparing the nums[mid] with nums[r] instead of nums[mid] ? nums[l], since I did comparison between mid and left for most of the problems. So here is the traditional way:

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

Log in to reply
 

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