9 lines C++, 8 ms


  • 0
    int searchhelp(vector<int>& nums, int target, int l, int r) {
        if (l==r) return l;
        int m = l+(r-l)/2;
        if (target <= nums[m]) return searchhelp(nums, target, l, m);
        else return searchhelp(nums, target, m+1, r);
    }
    int searchInsert(vector<int>& nums, int target) {
        return searchhelp(nums, target, 0, nums.size());
    }

Log in to reply
 

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