my understanding of (lower bound,upper bound) binary search, in C++, thanks to two post 分类: leetcode 2015-08-01 14:35 113人阅读 评论(0) 收藏
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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。// p.s. 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, in C++, thanks to two post 分类: leetcode 2015-08-01 14:35 113人阅读 评论(0) 收藏的更多相关文章
- POJ2566 Bound Found 2017-05-25 20:05 32人阅读 评论(0) 收藏
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4056 Accepted: 1249 Spe ...
- Binary Tree 分类: POJ 2015-06-12 20:34 17人阅读 评论(0) 收藏
Binary Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6355 Accepted: 2922 Descr ...
- Binary Indexed Tree 2D 分类: ACM TYPE 2014-09-01 08:40 95人阅读 评论(0) 收藏
#include <cstdio> #include <cstdlib> #include <climits> #include <cstring> # ...
- Binary Indexed Tree 分类: ACM TYPE 2014-08-29 13:08 99人阅读 评论(0) 收藏
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int n, ...
- 数据结构之Binary Search Tree (Java)
二叉查找树简介 二叉查找树(Binary Search Tree), 也成二叉搜索树.有序二叉树(ordered binary tree).排序二叉树(sorted binary tree), 是指一 ...
- [LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...
- 二分查找里的upper bound与lower bound的实现与分析
1. 问题引入 最近参选了学堂在线的课程数据结构(2015秋).课程由清华大学的邓俊辉老师主讲,在完成课后作业时,遇到了这样一个题目范围查询.在这个题目中,我需要解决这样一个子问题:给定了一组已经排好 ...
- PKU2018校赛 H题 Safe Upper Bound
http://poj.openjudge.cn/practice/C18H 题目 算平均数用到公式\[\bar{x}=\frac{x_1+x_2+x_3+\cdots+x_n}{n}\] 但如果用in ...
随机推荐
- (转载)MongoDB C#驱动中Query几个方法
MongoDB C#驱动中Query几个方法 Query.All("name", "a", "b");//通过多个元素来匹配数组 Query ...
- 如何解决sublime text 2总是在新窗口中打开文件及文件夹
如何在原窗口打开文件 具体设置:Preferences -> Settings – Default -> 搜索open_files_in_new_window,将其true 改为 fals ...
- python成长之路【第十二篇】:RabbitMQ入门
一.RabbitMQ介绍 解释RabbitMQ,就不得不提到AMQP(Advanced Message Queuing Protocol)协议. AMQP协议是一种基于网络的消息传输协议,它能够在应用 ...
- 装逼名词 bottom-half,软中断,preemptive选项
bottom-half http://bbs.csdn.net/topics/60226240 在中断,异常和系统调用里看Linux中断服务一般都是在关闭中断的情况下执行的,以避免嵌套而是控制复杂化L ...
- iOS 调试问题汇总
// PBXcp error修复-No such file or directory (2013-05-02 15:20:50) 差不多算是Xcode比较常见的一个编译错误了,原因往往是添加或删除美术 ...
- JSP隐藏过长字段
<div class="objDiv" title="${fof.fundName }"> <c ...
- 【J-meter】变量加密之Bean shell使用
参考资料: http://www.cnblogs.com/puresoul/p/4915350.html http://www.cnblogs.com/tester-hehehe/p/5466364. ...
- 媒体查询使用方法@media
Media Queries能在不同的条件下使用不同的样式,使页面在不同在终端设备下达到不同的渲染效果.前面简单的介绍了Media Queries如何引用到项目中,但Media Queries有其自己的 ...
- js 根据不同情况引入不同操作
1.根据分辨率不同引入不同的JS // 根据不同的分辨率引入不同的JS代码 <script> if (screen.width == 1440 && screen.heig ...
- 网站整站下载工具—HTTrack Website Copier
HTTrack是一个免费和易用的离线浏览工具(浏览器),它可以允许你下载整个WWW网站至本地目录,并且通过遍历网站目录获取HTML,图片和其他文件,是安全渗透测试和居家旅行必备软件. WinHTTra ...