My simple java solution


  • 1
    A
    public int searchInsert(int[] nums, int target) {
        if(nums==null) return 0;
        int i = 0;
        int j = nums.length-1;
        while (i<=j)
        {
            int mid = (i+j)/2;
            if(nums[mid]==target)
                return mid;
            else if(nums[mid]>target)
                j = mid-1;
            else 
                i = mid+1;
        }
        return Math.min(i,j)+1;
    }

Log in to reply
 

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