Simple Recursive Java BinarySearch


  • 2
    N
            public class Solution {
    
    private int[] helper(int[] nums, int start, int end, int target, int[] op){
        while(start <= end){
            int mid = start + ((end-start)/2);
            if(nums[mid] == target){
                op[0] = (op[0]==-1)?mid:Math.min(op[0], mid);
                op[1] = (op[1]==-1)?mid:Math.max(op[1], mid);
                //perform binary search for left of mid
                //cos the target might be a duplicate
                op = helper(nums, start, mid-1, target, op);
                //perform binary search for right of mid
                //cos the target might be a duplicate
                op = helper(nums, mid+1, end, target, op);
                return op;
            }else if(nums[mid] > target){
                end = mid-1;
            }else{
                start = mid +1;
            }
        }
        return op;
    }    
    
    public int[] searchRange(int[] nums, int target) {
        int[] op = {-1,-1};
        return helper(nums, 0, nums.length-1, target, op);
    }
    

    }


Log in to reply
 

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