You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First I want to say this is an excellent job! Thank you for sharing this!
At page 16, for question 34. Find First and Last Position of Element in Sorted Array (Medium), the function lower_bound below
may have a bug:
intlower_bound(vector<int> &nums, int target) {
int l = 0, r = nums.size(), mid;
while (l < r) {
mid = (l + r) / 2; // Here, may be overflow when r = MAX_INT and l= MAX_INT/2if (nums[mid] >= target) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
mid = (l + r) / 2; This code may be overflow when r = MAX_INT and l= MAX_INT/2, you should use mid = (r-l)/2+l instead.
Also the same for the upper_bound function.
The text was updated successfully, but these errors were encountered:
First I want to say this is an excellent job! Thank you for sharing this!
At page 16, for question
34. Find First and Last Position of Element in Sorted Array (Medium)
, the functionlower_bound
belowmay have a bug:
mid = (l + r) / 2;
This code may be overflow when r = MAX_INT and l= MAX_INT/2, you should usemid = (r-l)/2+l
instead.Also the same for the
upper_bound
function.The text was updated successfully, but these errors were encountered: