So concise code in c++ in O(long(n)) time

  • 0
     int helper(vector<int>&nums,int left, int right, int target){
       if (left > right) return left;
       int mid = (left+right)/2;
       if(nums[mid]==target) return mid;
       else if (nums[mid]<target) return helper(nums, mid+1,right,target);
       else return helper(nums, left, mid-1, target);
    int searchInsert(vector<int>& nums, int target) {
        if (target > nums.back()) return nums.size();
        return helper(nums, 0, nums.size(), target);

Log in to reply

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