Solution in Java_ddd


  • 0
    D

    ...
    class Solution {
    public int searchInsert(int[] nums, int target) {
    int i = 0;
    int j = nums.length - 1;
    while (i <= j) {
    if (nums[(i + j) / 2] > target) {
    j = (i + j) / 2 - 1;
    } else if (nums[(i + j) / 2] < target) {
    i = (i + j) / 2 + 1;
    } else {
    return (i + j) / 2;
    }
    }
    return i;
    }
    }
    ...


Log in to reply
 

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