See Arrays.binarySearch(int[], int)
.
class Solution {
public int searchInsert(int[] nums, int target) {
// Returns index of the search key, otherwise if not found: (-(insertion point) - 1)
int index = Arrays.binarySearch(nums, target);
return index < 0 ? (-index - 1) : index;
}
}
Questions? Have a neat solution? Comment below!