Search Insert Position 2021-04-18 07:48

Problem Description

public static int searchInsert(int[] nums, int target) {
    int left = 0;
    int right = nums.length - 1;
    while (left < right) {
        int mid = (left + right) / 2;
        if (nums[mid] == target) {
            return mid;
        }
        if (nums[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    if (nums[left] >= target) {
        return left;
    }
    return left + 1;
}
Runtime Memory
0 ms 38.4 MB

henryxi leetcode list

EOF