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. wcf开启服务 HTTP 无法注册 URL 进程不具有此命名空间的访问权限

    HTTP 无法注册 URL [url]http://127.0.0.1:9999/calculatorservice/metadata[/url].进程不具有此命名空间的访问权限 今天按照网上的例子开 ...

  2. HTML5 CANVAS 实现图片压缩和裁切

    原文地址:http://leonshi.com/2015/10/31/html5-canvas-image-compress-crop/?utm_source=tuicool&utm_medi ...

  3. Android微信智能心跳方案 (转)

    原创 2015-08-17 phoenix WeMobileDev 前言:在13年11月中旬时,因为基础组件组人手紧张,Leo安排我和春哥去广州轮岗支援.刚到广州的时候,Ray让我和春哥对Line和W ...

  4. Android的系统体系结构

    目录: Android的系统体系结构 Android的四种常用组件 Activity的启动流程 Android的系统体系结构 在入门了一个简单的Android的Hello World以后,我们首先来看 ...

  5. web.xml listener和event

    Listener接口 Event类 ServletContextListener ServletContextEvent ServletContextAttributeListener Servlet ...

  6. PHP 中:: -> self $this 操作符的区别

    访问PHP类中的成员变量或方法时, 如果被引用的变量或者方法被声明成const(定义常量)或者static(声明静态),那么就必须使用操作符::, 反之如果被引用的变量或者方法没有被声明成 const ...

  7. #pragma pack(push,1)与#pragma pack(1)的区别

    这是给编译器用的参数设置,有关结构体字节对齐方式设置, #pragma pack是指定数据在内存中的对齐方式. #pragma pack (n)             作用:C编译器将按照n个字节对 ...

  8. JAVA线程安全总结(转载)

    JAVA线程安全总结(一) JAVA线程安全总结(二) 最近想将java基础的一些东西都整理整理,写下来,这是对知识的总结,也是一种乐趣.已经拟好了提纲,大概分为这几个主题: java线程安全,jav ...

  9. 错误 java.lang.ClassCastException: com.ylpw.sms.YZZYSenderUtil cannot be cast to ResourceBundle

    出现错误: java.lang.ClassCastException: com.ylpw.sms.YZZYSenderUtil cannot be cast to ResourceBundle 百度搜 ...

  10. 使用wait()与notify()实现线程间协作

    调用sleep()和yield()的时候锁并没有被释放,而调用wait()将释放锁.这样另一个任务(线程)可以获得当前对象的锁,从而进入它的synchronized方法中.可以通过notify()/n ...