my java solution use binary search 6ms


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

Log in to reply
 

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