Super Easy and concise Java Binary Search Log(n) solution


  • 0
    R

    Below is my concise Java binary Search solution:
    I chose to compare the mid value with the end value, so that my code could handle all test cases. Hope you would like it:

    public int findMin(int[] nums) {
            int i = 0;
            int j = nums.length - 1;
            while(i < j) {
                int mid = (j - i)/2 + i;
                if (nums[mid] < nums[j]) {
                    j = mid;
                } else {
                    i = mid + 1;
                }
            }
            return nums[j];
        }
    

Log in to reply
 

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