My solution on java


  • 0
    T
    public class Solution {
           public int searchInsert(int[] nums, int target) {
           int size=nums.length;
           return searchInsert(nums, 0, size-1, target);  
        }
           private int searchInsert(int[] nums,int begin,int end,int target)
        {
        	if(begin<=end)
        	{
        		int mid=begin+(end-begin)/2;
        		
        		if(target<nums[mid])
        		{
    //    			end=mid;
        			
        			if(mid-1>=0)
        			return searchInsert(nums, begin, mid-1, target);
        			else 
        			{
        				if(target<nums[0])
        					return 0;
        				else return 1;
        			}
        		}
        		else if(target>nums[mid])
        		{
    //    			begin=mid+1;
        			return searchInsert(nums, mid+1, end, target);
        		}
        		else 
        			return mid;
        	}
        	
        	else{
        		System.out.println(begin);
        		if(target<nums[begin-1])
        		{
        			
        			return begin-1;
        		}
        		else 
        		{
        			
        			return begin;}
        	}
        }
        	
    }

Log in to reply
 

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