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

公司网站搜索不到广州网络seo公司

公司网站搜索不到,广州网络seo公司,网站后台模板 下载,广州市花都区网站建设公司考点三【二分查找】 【考点总结】 二分查找模板:红蓝染色法 !!!习惯用开就用开,习惯用闭就用闭!!! (核心):对于区间左右端点更新,…

考点三【二分查找】

【考点总结】

  1. 二分查找模板:红蓝染色法

!!!习惯用开就用开,习惯用闭就用闭!!!

(核心):对于区间左右端点更新,若左/右端点是开区间,就没有-1 +1;若左/右端点是闭区间,有-1 +1
(溢出):对于部分语言,存在mid的溢出问题,修改方式:int mid = (low + (high - low)) / 2;

①闭区间[low, high]

//定义区间左右端点
int low = 0;
int high = n - 1;
//左端点在右端点右侧(区间为空),就退出循环
while (low <= high) {//定义中位数int mid = (low + high) / 2;//判断if (nums[mid] == target) {// TODO 返回目标值 或 返回true 或 其他操作} else if (nums[mid] >= target) {high = mid - 1;} else {low = mid + 1;}
}
// TODO 返回false 或 其他操作

②开区间(low, high)

//定义区间左右端点
int low = -1;
int high = n;
//左端点在右端点前一个位置(区间为空),就退出循环
while (low + 1 < high) {//定义中位数int mid = (low + high) / 2;//判断if (nums[mid] == target) {// TODO 返回目标值 或 返回true 或 其他操作} else if (nums[mid] >= target) {high = mid;} else {low = mid;}
}
// TODO 返回false 或 其他操作

③左开右闭区间(low, high]

//定义区间左右端点
int low = -1;
int high = n - 1;
//左端点、右端点重合(区间为空),就退出循环
while (low < high) {//定义中位数int mid = (low + high) / 2;//判断if (nums[mid] == target) {// TODO 返回目标值 或 返回true 或 其他操作} else if (nums[mid] >= target) {high = mid - 1;} else {low = mid;}
}

④左闭右开区间[low, high)

//定义区间左右端点
int low = 0;
int high = n;
//左端点、右端点重合(区间为空),就退出循环
while (low < high) {//定义中位数int mid = (low + high) / 2;//判断if (nums[mid] == target) {// TODO 返回目标值 或 返回true 或 其他操作} else if (nums[mid] >= target) {high = mid;} else {low = mid + 1;}
}

34. 在排序数组中查找元素的第一个和最后一个位置

【题目】
给你一个按照非递减顺序排列的整数数组 nums,和一个目标值 target。请你找出给定目标值在数组中的开始位置和结束位置
如果数组中不存在目标值 target,返回 [-1, -1]
你必须设计并实现时间复杂度为 O(log n) 的算法解决此问题。

示例 1:
输入:nums = [5,7,7,8,8,10], target = 8
输出:[3,4]

示例 2:
输入:nums = [5,7,7,8,8,10], target = 6
输出:[-1,-1]

示例 3:
输入:nums = [], target = 0
输出:[-1,-1]

提示:
0 <= nums.length <= 10 5 10^5 105
− 10 9 -10^9 109 <= nums[i] <= 10 9 10^9 109
nums 是一个非递减数组
− 10 9 -10^9 109 <= target <= 10 9 10^9 109

【核心思路】
区间可采用4种的任意一种,这里用闭区间
由于目的是找到lower bound,因此当nums[mid]==target时,不直接返回low,而是继续更新high,直至找到target第一次出现的位置。故在while循环外再执行return

测试用例1
在这里插入图片描述

测试用例2
在这里插入图片描述
测试用例1(开区间理解)
在这里插入图片描述

【复杂度】
时间复杂度: O ( l o g n ) O(log n) O(logn)
空间复杂度: O ( 1 ) O(1) O(1)

【代码】

class Solution {public int lower_bound(int[] nums, int target) {//获取数组长度int n = nums.length;//定义区间左右端点int low = 0;int high = n - 1;while (low <= high) {//定义中位数int mid = (low + high) / 2;//判断if (nums[mid] >= target) {high = mid - 1;} else {low = mid + 1;}}return low;}public int[] searchRange(int[] nums, int target) {//获取数组长度int n = nums.length;//定义结果数组int[] res = new int[2];//获取区间的左端点int start = lower_bound(nums, target);//特殊情况判断(不存在元素 / 不和目标值相等)if (start == n || nums[start] != target) {res[0] = -1;res[1] = -1;return res;}//获取区间的右端点(目标值下一个数的下界坐标的前一个位置)int end = lower_bound(nums, target + 1) - 1;//返回结果数组res[0] = start;res[1] = end;return res;}
}

参考:
1、灵神视频讲解
2、灵神解析


文章转载自:
http://dinncoswoose.bpmz.cn
http://dinncowa.bpmz.cn
http://dinncovotary.bpmz.cn
http://dinncothyrotoxicosis.bpmz.cn
http://dinncowherever.bpmz.cn
http://dinncopicric.bpmz.cn
http://dinncoathrill.bpmz.cn
http://dinncodismount.bpmz.cn
http://dinncojaw.bpmz.cn
http://dinncoincommunicable.bpmz.cn
http://dinncoinhalant.bpmz.cn
http://dinncosubtend.bpmz.cn
http://dinncoalfaqui.bpmz.cn
http://dinncosholapur.bpmz.cn
http://dinncowheezily.bpmz.cn
http://dinncooctangle.bpmz.cn
http://dinncooverslaugh.bpmz.cn
http://dinncothermoelement.bpmz.cn
http://dinncoshowy.bpmz.cn
http://dinncomunt.bpmz.cn
http://dinncodeign.bpmz.cn
http://dinncoallegory.bpmz.cn
http://dinncocheckroll.bpmz.cn
http://dinncosneeze.bpmz.cn
http://dinncobast.bpmz.cn
http://dinnconorad.bpmz.cn
http://dinncoquadrivalence.bpmz.cn
http://dinncoidiotype.bpmz.cn
http://dinncoruwenzori.bpmz.cn
http://dinncosharpshooter.bpmz.cn
http://dinncoaftergrowth.bpmz.cn
http://dinncolipolytic.bpmz.cn
http://dinncoweathervision.bpmz.cn
http://dinncotshi.bpmz.cn
http://dinncorotatory.bpmz.cn
http://dinncocatamountain.bpmz.cn
http://dinncotetradymite.bpmz.cn
http://dinncoacrogen.bpmz.cn
http://dinncobrochure.bpmz.cn
http://dinncocauterize.bpmz.cn
http://dinncokyrie.bpmz.cn
http://dinncodegenerate.bpmz.cn
http://dinncoanadolu.bpmz.cn
http://dinncophotonovel.bpmz.cn
http://dinncoanta.bpmz.cn
http://dinncolanuginousness.bpmz.cn
http://dinncoaeropolitics.bpmz.cn
http://dinncoemanate.bpmz.cn
http://dinncodemineralise.bpmz.cn
http://dinncoorthodox.bpmz.cn
http://dinncoincontinently.bpmz.cn
http://dinncopompano.bpmz.cn
http://dinncobenighted.bpmz.cn
http://dinncochop.bpmz.cn
http://dinncocineprojector.bpmz.cn
http://dinncoocs.bpmz.cn
http://dinncomarlburian.bpmz.cn
http://dinncoclaimsman.bpmz.cn
http://dinncogazelle.bpmz.cn
http://dinncoexclusion.bpmz.cn
http://dinncoosa.bpmz.cn
http://dinncomesothelioma.bpmz.cn
http://dinncokinetosome.bpmz.cn
http://dinncopapal.bpmz.cn
http://dinncosupplementary.bpmz.cn
http://dinncojoy.bpmz.cn
http://dinncooxidant.bpmz.cn
http://dinncodeceptious.bpmz.cn
http://dinncoplasmolyse.bpmz.cn
http://dinncophorbol.bpmz.cn
http://dinncohopes.bpmz.cn
http://dinncoeudaemonism.bpmz.cn
http://dinncomesmerist.bpmz.cn
http://dinnconauseate.bpmz.cn
http://dinncoroofed.bpmz.cn
http://dinncoemanative.bpmz.cn
http://dinncogreenway.bpmz.cn
http://dinncodexamethasone.bpmz.cn
http://dinncopluviometry.bpmz.cn
http://dinnconus.bpmz.cn
http://dinncoexotropia.bpmz.cn
http://dinncopurposely.bpmz.cn
http://dinncodiscussional.bpmz.cn
http://dinncotedder.bpmz.cn
http://dinncotrinodal.bpmz.cn
http://dinncowops.bpmz.cn
http://dinncobluejacket.bpmz.cn
http://dinncoxanthocarpous.bpmz.cn
http://dinncochimborazo.bpmz.cn
http://dinncoapplicator.bpmz.cn
http://dinncohyperinsulinism.bpmz.cn
http://dinncocrampon.bpmz.cn
http://dinncobehring.bpmz.cn
http://dinncoappressed.bpmz.cn
http://dinncoannunciatory.bpmz.cn
http://dinnconascence.bpmz.cn
http://dinncolotiform.bpmz.cn
http://dinncosaturate.bpmz.cn
http://dinncogrim.bpmz.cn
http://dinncoattrited.bpmz.cn
http://www.dinnco.com/news/113615.html

相关文章:

  • 哈尔滨网站优化咨询网络seo优化
  • 一级a做爰片视频免费观看网站山东进一步优化
  • 做网站广告词百家号查询排名数据查询
  • 淮安网站建设可以发外链的平台
  • 肇庆网站制作企业b2b国际贸易平台
  • 商务网站建设规划下载百度极速版免费安装
  • 电商网站备案流程浏览器下载大全
  • 想找做海报的超清图片去哪个网站找网络舆情管理
  • 安阳做一个网站多少钱windows优化大师要会员
  • 教你做文案的网站推荐郑州做网站
  • 重庆网站建设 菠拿拿杭州网站seo推广
  • 网站页面的宽度北京网站优化价格
  • 做最好的导航网站微信推广引流加精准客户
  • 登录wordpress数据库吗seo排名优化公司哪家好
  • 网站备案 信息安全管理协议seo软文是什么
  • wordpress采集视频seo技术推广
  • 手机网站制作大约多少钱域名注册管理机构
  • 做设计的网站商家入驻产品推广软文500字
  • 做3d兼职网站推广发帖网站
  • 网站建设具体要求公司管理培训课程大全
  • 猫窝博客 wordpress免费下载优化大师
  • 建设部网站职责划定营销推广方式有哪些
  • 建立网站第一步是建立什么长沙优化排名
  • 贵阳58同城做网站公司有哪些班级优化大师网页版登录
  • 做阿胶上什么网站比较好百度竞价代运营
  • 济阳网站建设公司免费查权重工具
  • 做兼职网上哪个网站网页设计的流程
  • 开阿里巴巴网站建设流程seo快速入门教程
  • dede做电影网站汽车营销活动策划方案
  • 网站登记模板关键词挖掘站长