lower_bound当target存在时, 返回它出现的第一个位置,如果不存在,则返回这样一个下标i:在此处插入target后,序列仍然有序。

代码如下:

int lower_bound(int* nums, int numsSize, int target) {
//注意left和right的初始值必须是left = 0, right = numsSzie, 因为插入的位置可能是[0,numsSize]
int left = 0;
int right = numsSize;
int mid;
while (left < right)
{
mid = left + (right - left) / 2;
if (nums[mid] >= target)
right = mid;
else
left = mid + 1;
} return left;
}

尽管查找区间是[0, numsSize),但返回值区间却是[0, numsSize]。因此right的初始值必须为numsSize,而不是numsSize - 1。

当nums[mid] == target时,至少已经找到了一个,而左边可能还有,因此区间变为[left, mid];

当nums[mid] > target时,所求位置不可能在后面,但有可能是mid,因此区间变为[left, mid];

当nums[mid] < target时,mid和前面都不可行,因此所求区间为[mid+1, right]。

合并一下,当nums[mid] >= target时,所求区间为[left, mid];当nums[mid] < target时,所求区间为[mid + 1, right]。

类似地,可以写一个upper_bound程序,当target存在时, 返回它出现的最后一个位置的后面一个位置,如果不存在,则返回这样一个下标i:在此处插入target后,序列仍然有序。

int upper_bound(int* nums, int numsSize, int target) {
//注意left和right的初始值必须是left = 0, right = numsSzie, 因为插入的位置可能是[0,numsSize]
int left = 0;
int right = numsSize;
int mid;
while (left < right)
{
mid = left + (right - left) / 2;
if (nums[mid] <= target)
left = mid + 1;
else
right = mid;
}
return left;
}

二分查找确定lower_bound和upper_bound的更多相关文章

  1. C++二分查找:lower_bound( )和upper_bound( )

    #include<algorithm>//头文件 //标准形式 lower_bound(int* first,int* last,val); upper_bound(int* first, ...

  2. 二分查找(lower_bound和upper_bound)

    转载自:https://www.cnblogs.com/luoxn28/p/5767571.html 1 二分查找 二分查找是一个基础的算法,也是面试中常考的一个知识点.二分查找就是将查找的键和子数组 ...

  3. 二分检索函数lower_bound()和upper_bound()

    二分检索函数lower_bound()和upper_bound() 一.说明 头文件:<algorithm> 二分检索函数lower_bound()和upper_bound() lower ...

  4. STL_算法_查找算法(lower_bound、upper_bound、equal_range)

    C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) //全部容器适用(O(log(n)))    已序区间查找算法 lower_bound()        //找第一个符合的 ...

  5. 关于二分查找 使用 lower_bound

    在寻找单调递增最长自序列 , 的时候能不能确认出来哪个是单调递增最长自序列  ?  我的想法是 if(location>=num) dp[location]=b; 这样的 , 基于http:// ...

  6. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  7. 【CodeForces-1041C】Coffee Break(二分解决关于set,pair,upper_bound用法)

    //题意:一个的工作时间是m分钟. // 在特定的时间和咖啡 n a1,a2....an,, ai代表的是每个咖啡要在一天中对应的时间点喝掉 // 每一次喝咖啡的时间为1分钟 // 必须在一天中的ai ...

  8. I Count Two Three(打表+排序+二分查找)

    I Count Two Three 二分查找用lower_bound 这道题用cin,cout会超时... AC代码: /* */ # include <iostream> # inclu ...

  9. STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())

    一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search  -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...

随机推荐

  1. HarmonyOS三方件开发指南(14)-Glide组件功能介绍

    <HarmonyOS三方件开发指南>系列文章合集 引言 在实际应用开发中,会用到大量图片处理,如:网络图片.本地图片.应用资源.二进制流.Uri对象等,虽然官方提供了PixelMap进行图 ...

  2. 快速创建你的第一个Spring Boot项目

    1. 创建工程 打开idea,利用Spring Boot搭建一个web工程,切身体会一下Spring Boot所带来的魅力!看看SpringBoot是如何快速搭建一个web项目. New-->P ...

  3. asp.net core 2.0 web api + Identity Server 4 + angular 5 可运行前后台源码

    前台使用angular 5, 后台是asp.net core 2.0 web api + identity server 4. 从头编写asp.net core 2.0 web api 基础框架: 第 ...

  4. QT项目-Chart Themes Example学习(一)

    1.main.cpp #include "themewidget.h" #include <QtWidgets/QApplication> #include <Q ...

  5. PAT (Basic Level) Practice (中文)1054 求平均值 (20 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1054 求平均值 (20 分) 题目描述 本题的基本要求非常简单:给定 N 个实数,计算它们的平均值.但复杂的是有些输入数据可能是非法的 ...

  6. Go Protobuf(比xml小3-10倍, 快20-100倍)

    简介 Protocol Buffers是什么? protocol buffers 是一种灵活,高效,自动化机制的结构数据序列化方法-可类比 XML,但是比 XML 更小.更快.更为简单.你可以定义数据 ...

  7. python基础(五):列表的使用(上)

    什么是列表 列表是一系列元素,按特定顺序排列组成.列表总的元素之间没有任何关系,既可以时字符串,也可以是数字,还可以是布尔值. 由此可以看出,列表通常包含多个元素,因此再给列表命名的时候,最好使用复数 ...

  8. 记一次phpwind的漏洞测试学习

    实验:phpwind的文件目录遍历 工具:windows2003,Windows10,phpstudy2018,phpwind8.7 在Windows2003中,安装phpstudy并且部署phpwi ...

  9. Java 常用类总结(SE基础)

    本篇博客对java常用类相关知识进行了归纳总结,比较详细,适用于学习和复习. 1. 字符串相关的类 1.1 String String是一个final类,代表不可变的字符序列.不可被继承. Strin ...

  10. Algorithm(4th) 1.5 union-find算法

    问题描述 问题输入是一对整数对,每个整数都代表一个对象,一对整数"p,q"表示 "p与q相连"(具有自反性,传递性,对称性,被归到一个等价类里),要求编写程序来 ...