JAVA 0ms binary search solution


  • 0
    C
            int start = 0, end = nums.length-1, mid = start;
            if (end == -1) {
                return -1;
            }
            while (start < end){
                mid = start + (end-start)/2;
                // The middle value is larger than end, means the smaller value belongs to the latter part, exclude the middle value
                if (nums[mid] > nums[end]) {
                    start = mid+1;
                }
                // Otherwise, the smaller value belongs to the former part, including the middle value
                else {
                    end = mid;
                }
            }
            return nums[start];
        }

Log in to reply
 

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