Easy java solution


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

Log in to reply
 

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