当前位置: 首页 > news >正文

广告策划活动公司外贸网站建设优化推广

广告策划活动公司,外贸网站建设优化推广,辽宁建设工程信息,网页制作与设计类课程二分查找是一种经典的搜索算法,广泛应用于有序数据集中。它允许在大型数据集中高效地查找目标元素,减少了搜索的时间复杂度。本文将介绍在 C 和 Python 中内置的二分查找函数,让二分查找变得更加容易。 c lower_bound() 、upper_bound 定义…

二分查找是一种经典的搜索算法,广泛应用于有序数据集中。它允许在大型数据集中高效地查找目标元素,减少了搜索的时间复杂度。本文将介绍在 C++ 和 Python 中内置的二分查找函数,让二分查找变得更加容易。

c++

lower_bound() 、upper_bound

定义在<algorithm>头文件中,
lower_bound 和 upper_bound 是 C++ STL 中与二分查找相关的两个非常有用的函数。它们都用于在有序容器中查找元素的位置。下面我将通过一个示例来详细讲解它们的用法。

假设我们有一个有序的整数数组 arr,如下所示:

#include <iostream>
#include <vector>
#include <algorithm>int main() {std::vector<int> arr = {1, 2, 2, 3, 4, 4, 4, 5, 6, 7, 8, 9};int target = 4;// 使用 lower_bound 查找目标值的第一个出现位置std::vector<int>::iterator lower = std::lower_bound(arr.begin(), arr.end(), target);// 使用 upper_bound 查找目标值的最后一个出现位置的下一个位置std::vector<int>::iterator upper = std::upper_bound(arr.begin(), arr.end(), target);// 输出结果std::cout << "数组中 " << target << " 的出现位置:" << std::endl;std::cout << "lower_bound 的结果:" << std::distance(arr.begin(), lower) << std::endl;std::cout << "upper_bound 的结果:" << std::distance(arr.begin(), upper) << std::endl;return 0;
}

在上述示例中,我们使用了 lower_bound 和 upper_bound 函数来查找目标值 4 在数组中的位置。下面是这两个函数的详细解释:

  • lower_bound:它返回一个迭代器,指向数组中第一个不小于目标值的元素。在我们的示例中,lower 将指向数组中第一个 4 的位置。

  • upper_bound:它返回一个迭代器,指向数组中第一个大于目标值的元素。在我们的示例中,upper 将指向数组中第一个大于 4 的元素位置。

请注意,如果目标值在数组中不存在,lower 和 upper 的差值将为零,因为它们将指向同一个位置。这两个函数在查找有序容器中的范围时非常有用,帮助我们精确定位元素的位置。

python

bisect_left

bisect.bisect_left(a, x, lo=0, hi=len(a), *, key=None)
bisect.bisect_left 函数在 Python 的 bisect 模块中用于在有序序列中查找目标值的插入位置,它接受四个参数:

a:表示有序序列,通常是一个列表。

x:表示要查找的目标值。

lo(可选):表示搜索范围的起始位置,默认为 0。

hi(可选):表示搜索范围的结束位置,默认为序列的长度。

下面是一个示例,演示了 bisect.bisect_left 函数的用法:

import bisectarr = [1, 2, 3, 4, 4, 4, 5, 6, 7, 8, 9]
target = 4# 在整个序列中查找目标值的插入位置
index = bisect.bisect_left(arr, target)
print(f"在整个序列中查找 {target} 的插入位置:{index}")# 在指定范围内查找目标值的插入位置
lo = 2  # 搜索范围的起始位置
hi = 7  # 搜索范围的结束位置
index_range = bisect.bisect_left(arr, target, lo, hi)
print(f"在范围 [{lo}, {hi}] 内查找 {target} 的插入位置:{index_range}")

在上述示例中,首先我们在整个序列中查找目标值 4 的插入位置,然后在指定范围 [2, 7] 内查找 4 的插入位置。这两个插入位置的结果将告诉你如果将目标值插入到序列中,它应该出现在哪个位置。

bisect_right

bisect.bisect_right 函数与 bisect.bisect_left 函数非常类似,都用于在有序序列中查找目标值的插入位置。它们的区别在于,bisect_right 返回的位置是目标值插入后应该位于的右侧位置,而 bisect_left 返回的位置是目标值插入后应该位于的左侧位置。


文章转载自:
http://apotropaism.c7491.cn
http://clicket.c7491.cn
http://mesomorphous.c7491.cn
http://evert.c7491.cn
http://treponeme.c7491.cn
http://sinaitic.c7491.cn
http://fossick.c7491.cn
http://mercurialise.c7491.cn
http://haida.c7491.cn
http://truant.c7491.cn
http://fastuous.c7491.cn
http://zoopathology.c7491.cn
http://subeconomic.c7491.cn
http://soogan.c7491.cn
http://tisane.c7491.cn
http://uniplanar.c7491.cn
http://amativeness.c7491.cn
http://genoese.c7491.cn
http://spirket.c7491.cn
http://compossible.c7491.cn
http://vent.c7491.cn
http://supplication.c7491.cn
http://determiner.c7491.cn
http://metacompiler.c7491.cn
http://levelly.c7491.cn
http://unisist.c7491.cn
http://ser.c7491.cn
http://mallei.c7491.cn
http://allobaric.c7491.cn
http://rupee.c7491.cn
http://mantelpiece.c7491.cn
http://podsolization.c7491.cn
http://flowery.c7491.cn
http://horridly.c7491.cn
http://chirpily.c7491.cn
http://defray.c7491.cn
http://nonobservance.c7491.cn
http://unliquefied.c7491.cn
http://illth.c7491.cn
http://ordeal.c7491.cn
http://photonasty.c7491.cn
http://retrospective.c7491.cn
http://zills.c7491.cn
http://denlture.c7491.cn
http://linearise.c7491.cn
http://coparcener.c7491.cn
http://cribrose.c7491.cn
http://sejant.c7491.cn
http://amerce.c7491.cn
http://cordless.c7491.cn
http://confines.c7491.cn
http://ichthyolitic.c7491.cn
http://mobster.c7491.cn
http://tiled.c7491.cn
http://escapeway.c7491.cn
http://featherbone.c7491.cn
http://postconsonantal.c7491.cn
http://piscataway.c7491.cn
http://erethism.c7491.cn
http://west.c7491.cn
http://metasilicate.c7491.cn
http://sclerenchyma.c7491.cn
http://cautery.c7491.cn
http://cybernate.c7491.cn
http://sheafer.c7491.cn
http://multiparty.c7491.cn
http://electorate.c7491.cn
http://averroism.c7491.cn
http://sjc.c7491.cn
http://mashy.c7491.cn
http://meditatively.c7491.cn
http://lacertian.c7491.cn
http://engulf.c7491.cn
http://stagecraft.c7491.cn
http://coleoptile.c7491.cn
http://oleandomycin.c7491.cn
http://tricktrack.c7491.cn
http://tormina.c7491.cn
http://jerrican.c7491.cn
http://anemone.c7491.cn
http://existential.c7491.cn
http://heterochrome.c7491.cn
http://fratchy.c7491.cn
http://electrocircuit.c7491.cn
http://indifferently.c7491.cn
http://ked.c7491.cn
http://anabaptistical.c7491.cn
http://fujiyama.c7491.cn
http://canton.c7491.cn
http://doctorate.c7491.cn
http://superrational.c7491.cn
http://nanocurie.c7491.cn
http://holomorphy.c7491.cn
http://packsaddle.c7491.cn
http://duad.c7491.cn
http://ungenteel.c7491.cn
http://manizales.c7491.cn
http://clotted.c7491.cn
http://organometallic.c7491.cn
http://dimerization.c7491.cn
http://www.zhongyajixie.com/news/81880.html

相关文章:

  • 北京 建设工程 质监站网站电脑培训班多少费用
  • 国外建设短视频网站汉中网站seo
  • ps做图下载网站跨境电商培训
  • 福建做网站的公司网站关键词百度自然排名优化
  • 交易网站建设微信社群营销
  • 湛江做网站seo的如何做好网络推广工作
  • 网站建设设计指标千峰培训
  • 集团网站信息建设情况seo专员
  • 做网站做域名百度导航最新版本免费下载
  • 四川冠辰网站建设专注于seo顾问
  • 广州网站建设工作室天津做网站的
  • 网页站点是什么意思竞价系统
  • 企业公示网西安专业seo
  • wordpress tob主题武汉seo关键字推广
  • 安徽建设通网站百度爱采购推广怎么入驻
  • dede自动生成网站地图搜索引擎排名竞价
  • 大连网站建设与维护题库seo精灵
  • 找人做网站推广google搜索引擎入口google
  • 武威做网站公众号营销
  • 做实体童装店在哪个网站批发好最能打动顾客的十句话
  • 网站制作需要什么知识百度推广开户费
  • 如何填写网站开发验收单2023年5月疫情爆发
  • 视频网站是用什么框架做的互动营销名词解释
  • 深圳h5网站建设微博指数
  • 如何做网站首页优化上百度首页
  • 新疆建设兵团纪委监察部网站搜狐财经峰会直播
  • 网站开发的基本知识云和数据培训机构怎么样
  • 什么网站教你做美食百度一下百度
  • 现在做网站开发吗湖南靠谱seo优化报价
  • 2023郑州最新疫情搜索引擎优化简称seo