my understanding of (lower bound,upper bound) binary search, in C++, thanks to two post
来源:互联网 发布:c4d python生成器 编辑:程序博客网 时间:2024/06/12 01:47
If you understand the comments below, never will you make mistakes with 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.
http://en.cppreference.com/w/cpp/algorithm/upper_bound
Returns an iterator pointing to the first element in the range [first, last) that is greater than value.
http://en.cppreference.com/w/cpp/algorithm/lower_bound
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, try https://leetcode.com/problems/search-insert-position/ , https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ , https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ and https://leetcode.com/problems/search-a-2d-matrix-ii/ , and see the discusses.
int binarySearch(vector<int>& nums, int target) { /// return index of first one that comp(item,target)==true, or nums.size() if not found /// comp is greater or equal to for lower_bound /// comp is greater for upper_bound int first=0, last=nums.size(), mid; while (first<last) { mid=first+((last-first)>>1); // first<=mid, mid<last /// if comp(item,target)==false, advance first // if(nums[mid]<=target) // for upper_bound if (nums[mid]<target) // for lower_bound first=mid+1; // first always increases else /// else recede last last=mid; // last always decreases (even last-first==1) } return first;}
def bsearch(seq, key): """ return index of first item in seq such that key(item) is true, if no such item exist, return len(seq) """ l, r = 0, len(seq) while l<r: mid = l+(r-l)/2 if key(seq[mid]): r = mid else: l = mid + 1 return r
- my understanding of (lower bound,upper bound) binary search, in C++, thanks to two post
- lower-bound和upper-bound
- SICP 习题2.7 实现区间的lower-bound 和 upper-bound
- lower_bound upper bound
- lower_bound upper bound 2
- hackerrank>Dashboard>C++>STL>Lower Bound-STL
- Lower Bound-STL
- Desugar Scala(16) -- Lower Bound
- Convert a String In C++ To Upper or Lower Case
- Convert a String In C++ To Upper or Lower Case
- What is the Cramer-Rao Lower Bound?
- poj2566 Bound Found -- Two pointers
- first step in order to optimization of my C program
- Binary Search:349. Intersection of Two Arrays
- My Understanding of MQSeries
- Linear Time Selection[An Ω(nlogn) Sorting Lower Bound]
- Find the nearest common ancestor of any two nodes in a binary search tree
- The core of cloud computing in my understanding
- 面向对象概述
- 1276 士兵队列训练问题【队列模拟】
- UE和UI的概念
- 提高iOS开发效率的方法和工具
- LA3357 Pinary (递推)
- my understanding of (lower bound,upper bound) binary search, in C++, thanks to two post
- java之ArrayList的toArray方法
- Hibernate内置映射
- windows的socket_tcp服务端与linux的socket_tcp客服端简单通讯
- 机试算法讲解: 第41题 广度优先搜索之迷宫问题
- 使用live writer发布typecho博客含图片文章,支持1.0
- zoj 1951 Goldbach's Conjecture(素数筛选继续水)
- qt学习之键盘事件( keyPressEvent)
- 【ASP.NET】——AdRotator控件