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) 收藏的更多相关文章

  1. POJ2566 Bound Found 2017-05-25 20:05 32人阅读 评论(0) 收藏

    Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4056   Accepted: 1249   Spe ...

  2. Binary Tree 分类: POJ 2015-06-12 20:34 17人阅读 评论(0) 收藏

    Binary Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6355   Accepted: 2922 Descr ...

  3. Binary Indexed Tree 2D 分类: ACM TYPE 2014-09-01 08:40 95人阅读 评论(0) 收藏

    #include <cstdio> #include <cstdlib> #include <climits> #include <cstring> # ...

  4. Binary Indexed Tree 分类: ACM TYPE 2014-08-29 13:08 99人阅读 评论(0) 收藏

    #include<iostream> #include<cstring> #include<cstdio> using namespace std; int n, ...

  5. 数据结构之Binary Search Tree (Java)

    二叉查找树简介 二叉查找树(Binary Search Tree), 也成二叉搜索树.有序二叉树(ordered binary tree).排序二叉树(sorted binary tree), 是指一 ...

  6. [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 ...

  7. 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]* ...

  8. 二分查找里的upper bound与lower bound的实现与分析

    1. 问题引入 最近参选了学堂在线的课程数据结构(2015秋).课程由清华大学的邓俊辉老师主讲,在完成课后作业时,遇到了这样一个题目范围查询.在这个题目中,我需要解决这样一个子问题:给定了一组已经排好 ...

  9. 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 ...

随机推荐

  1. 通用访问 - 用“反射”来设计通用的通信协议,以及配套的SDK、工具

    1. 效果演示 2. 通信协议 功能介绍 特点 TCP协议 WebApi协议 3. SDK与工具 4. 应用示例 迷你网管 通用GIS 系统管理 5. 设计初衷与演化   1. 效果演示     服务 ...

  2. Git 安装

    安装参考资料: http://lzw.me/a/msysgit-tortoisegit-win-git.html http://blog.csdn.net/qwiwuqo/article/detail ...

  3. TCP/IP 三次握手-四次挥手

    TCP的建立需要三次握手,断开需要四次挥手. 首先三次握手: 首先,客户机向服务器发送请求报文,服务器回复ACK,并分配资源,而客户端接受到ACK后回复确认报文,并分配资源,此时三次握手完成. 四次挥 ...

  4. node config

    @echo off title node_5560 cd /d %~dp0 java -jar selenium-server-standalone-.jar ^ -role node ^ -Dweb ...

  5. 在进行javaIO写文件操作后文件内容为空的情况

    writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:\\order.txt"))) ...

  6. 了解真实的『REM』手机屏幕适配

    rem 作为一个低调的长度单位,由于手机端网页的兴起,在屏幕适配中得到重用.使用 rem 前端开发者可以很方便的在各种屏幕尺寸下,通过等比缩放的方式达到设计图要求的效果. rem 的官方定义『The ...

  7. python——django使用mysql数据库(一)

    之前已经写过如何创建一个django项目,现在我们已经有了一个小骷髅,要想这个web工程变成一个有血有肉的人,我们还需要做很多操作.现在就先来介绍如何在django中使用mysql数据库. 前提:已经 ...

  8. Myeclipse的使用

    一,错误解决 1, 现象:使用eclipse运行带有main函数的Java文件时,出现editor does not contain a main type的错误框 原因:原来这个class所在包没有 ...

  9. 3d旋转

    <!DOCTYPE html><html lang="zh-cmn-Hans"><head><meta charset="utf ...

  10. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...