Clear Java Recursion Version


  • 0
    C
    public class Solution {
        public int[] searchRange(int[] nums, int target) {
            int[] range1 = new int[2];
            int[] range2 = new int[2];
            int[] range = new int[2];
            int[] no = {-1,-1};
            int[] zero = {0,0};
            if(nums[0]>target||nums[nums.length-1]<target){
            	
            	return no;
            }
            else if (nums.length == 1) {
            	if(nums[0]!=target){
            		return no;
            	}
            	else{
            		return zero;
            	}
            }
            else {
            	range1 = searchRange(Arrays.copyOfRange(nums,0,nums.length/2),target);
            	range2 = searchRange(Arrays.copyOfRange(nums,nums.length/2,nums.length),target);
            }
            if(range1[0]!=-1&&range2[0]==-1){
            	range = range1;
            }
            else if (range2[0]!=-1&&range1[0]==-1) {
            	range2[0]+=nums.length/2;
            	range2[1]+=nums.length/2;
            	range = range2;
        	}
            else if (range1[0]!=-1&&range2[0]!=-1) {
            	range[0] = range1[0];
            	range[1] = range2[1]+nums.length/2;
            }
            else {
            	range = no;
            }
            return range;
        }
    }
    

Log in to reply
 

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