JAVA------------------------Easy Version To UnderStand!!!!!!!!!!!!!!!!!!!!


  • 0
    H
    	public static boolean search(int[] nums, int target) {
    	if (nums.length == 0 || nums == null)
    		return false;
    	int low = 0, high = nums.length - 1, mid;
    	while (low <= high) {
    		mid = low + (high - low) / 2;
    		if (nums[mid] == target)
    			return true;
    		else if (nums[mid] > nums[low]) {
    			if (target >= nums[low] && target < nums[mid])
    				high = mid - 1;
    			else
    				low = mid + 1;
    		} else if (nums[mid] < nums[low]) {
    			if (target > nums[mid] && target <= nums[high])
    				low = mid + 1;
    			else
    				high = mid - 1;
    		} else {
    			low++;
    		}
    	}
    	return false;
    }

Log in to reply
 

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