Shortest Java Solution


  • 0
        public int searchInsert(int[] nums, int target) {
            if(target > nums[nums.length -1]) return nums.length;
            int start = -1, end = nums.length - 1;
            while(start != end - 1){
                int mid = start + (end - start) / 2;
                if(nums[mid] >= target) end = mid;
                else start = mid;
            }
            return end;
        }
    }
    

Log in to reply
 

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