C++ BS


  • 0
    L
        int binarySearch(vector<int>& nums, int target, int low, int high) {
            int mid = (low + high) / 2;
            if (low <= high) {
                if (nums[mid] == target)
                    return mid;
                else if (nums[mid] > target)
                    return binarySearch(nums, target, low, mid - 1);
                else
                    return binarySearch(nums, target, mid + 1, high);
            } else {
                if (target >= nums[nums.size() - 1])
                    return nums.size();
                if (target < nums[0])
                    return 0;
                return mid + 1;
            }
        }
        int searchInsert(vector<int>& nums, int target) {
            return binarySearch(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.