C++ solution using binary search


  • 3
     int searchInsert(vector<int>& nums, int target) {
            int begin = 0, end = nums.size()-1, mid = 0;
            while(begin<=end){
                mid = (begin+end)/2;
                if(nums[mid]<target) begin = (mid++)+1;
                else end = mid-1;
            }
            return mid;
        }
    

Log in to reply
 

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