Using binary search C++


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

Log in to reply
 

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