1ms Java recursive solution


  • -1
    F
        public boolean search(int[] nums, int target) {
        if (nums == null || nums.length == 0) return false;
        return search(nums, 0, nums.length - 1, target);
    }
    
    private boolean search(int[] arr, int from, int to, int target) {
        if (from > to) return false;
        if (arr[from] == target) return true;
        if (arr[to] == target) return true;
        int mid = (from + to) / 2;
        if (arr[mid] == target) return true;
        return search(arr, from, mid - 1, target) ? true : search(arr, mid + 1, to, target);
    }

Log in to reply
 

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