Java Binary Search Merged case solution short


  • 0
    L
                    
    public int search(int[] nums, int target) {
            int start = 0;
    	int end = nums.length - 1;
    	while (start <= end) {
    		int mid = start + (end - start) / 2;
    		if (nums[mid] == target) {
    			return mid;
    		}
    		if ((nums[mid] >= nums[end] && (target > nums[mid] || target <= nums[end])) || target > nums[mid] && target <= nums[end]) {
    			start = mid + 1;
    		} else {
    			end = mid - 1;
    		}
    	}
    	return -1;
    }
    

Log in to reply
 

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