my understanding of (lower bound,upper bound) binary search,

thanks to A simple CPP solution with lower_bound and C++ O(logn) Binary Search that handles duplicate, thanks to phu1ku ‘s answer on the second post. Returns an iterator pointing to the first element in the range [first, last) that is greater than value. Returns an iterator pointing to the first element in the range [first, last) that is not less than (i.e. greater or equal to) value. if want to practice, code on your own, https://leetcode.com/problems/search-insert-position/

int binarySearch(vector<int>& nums, int target) {first=0, last=nums.size(), mid;while (first<last) {mid=first+((last-first)>>(nums[mid]<target) // for lower_boundfirst=mid+last=mid; // last always decreases (even last-first==1)}return first;}

版权声明:本文为博主原创文章,,未经博主允许不得转载。// ps. If in any way improment can be achieved, better performance or whatever, it will be well-appreciated to let me know, thanks in advance.

联系朋友别欠费,天空辽阔任你飞,再多困难别后退! “

my understanding of (lower bound,upper bound) binary search,

相关文章:

你感兴趣的文章:

标签云: