Super clean and simple binary search Java code, commented.


  • 0
    J
    public int findMin(int[] nums) {
    	 int l = 0, r = nums.length-1;
    	 while (l < r) {
    		 int mid = (l + r) / 2;
    		 if (nums[mid] < nums[r]) {
    			//right side in order, and mid is smallest of right side,
    			// min should be mid or in left.
    			 r = mid;   
    		 } else {
    			//left side in order and it is the larger part,
    			// min should be in right side.
    			 l = mid + 1; 
    		 }
    	 }
    	 return nums[l];
    }

Log in to reply
 

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