public int searchInsert(int[] A, int target) {
int low = 0, high = A.length1;
while(low<=high){
int mid = (low+high)/2;
if(A[mid] == target) return mid;
else if(A[mid] > target) high = mid1;
else low = mid+1;
}
return low;
}
My 8 line Java solution

Another Java solution:
public class Solution { public int searchInsert(int[] nums, int target) { int s = 0, e = nums.length1; while(s < e){ int m = (s + e)/2; if(nums[m] < target) { s = m + 1; } else if(nums[m] > target) { e = m; } else return m; } return target > nums[s] ? s + 1: s; } }


@kaiChristopher
i saw someone use this: mid=left+(rightleft)/2 ,you can try this to prevent overflow problem

this my solution:
public static int searchInsert(int[] nums, int target) {
if (target<nums[0]) { return 0; } if (target>nums[nums.length1]) { return nums.length; } for (int i=0;i<nums.length;i++) { if (nums[i]==target) { return i; } if (target>nums[i]&&target<nums[i+1]) { return i+1; } } return 0; }


@AmmsA why declaration of var 'mid' is in the while loop ,it may waste more time than declaration in the front?

