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

南昌哪里做网站好网络营销的4p策略

南昌哪里做网站好,网络营销的4p策略,中山建网站多少钱,网站开发得花多少钱题目描述 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。 请必须使用时间复杂度为 O(log n) 的算法。 示例 1: 输入: nums [1,3,5,6], target 5 输出: 2 …

题目描述

给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。

请必须使用时间复杂度为 O(log n) 的算法。

示例 1:

输入: nums = [1,3,5,6], target = 5

输出: 2

示例 2:

输入: nums = [1,3,5,6], target = 2
输出: 1

示例 3:

输入: nums = [1,3,5,6], target = 7
输出: 4

解题思路

二分查找的时间复杂度是 O(log n),其中 n 是数组的长度。所以可实现一个二分查找算法,用于在排序数组中查找一个目标值,并返回目标值的索引或者它应该被插入的位置。

代码

/*** @param {number[]} nums* @param {number} target* @return {number}*/
var searchInsert = function(nums, target) {let left = 0, right = nums.length - 1; // 闭区间 [left, right]while (left <= right) { // 区间不为空// 循环不变量:// nums[left-1] < target// nums[right+1] >= targetconst mid = Math.floor((left + right) / 2);if (nums[mid] < target) {left = mid + 1; // 范围缩小到 [mid+1, right]} else {right = mid - 1; // 范围缩小到 [left, mid-1]}}return left;
}

代码分析

  1. 初始化两个指针 leftright,分别指向数组的起始和结束位置,形成一个闭区间 [left, right]

  2. 进入一个 while 循环,条件是 left 小于等于 right,即区间不为空。

  3. 在循环内部,计算中间位置 mid,使用 Math.floor((left + right) / 2) 来确保 mid 是一个整数。

  4. 比较 nums[mid]target 的值:

    • 如果 nums[mid] 小于 target,则说明 target 可能在 mid 的右侧,因此更新 leftmid + 1,这样新的搜索区间就变成了 [mid + 1, right]
    • 如果 nums[mid] 大于或等于 target,则说明 target 可能在 mid 的左侧或 mid 本身,因此更新 rightmid - 1,这样新的搜索区间就变成了 [left, mid - 1]
  5. while 循环结束时,left 指针将指向 target 应该被插入的位置。如果 target 在数组中存在,left 将指向 target 的索引;如果 target 不存在,left 将指向 target 应该被插入的位置,以保持数组的排序。

  6. 最后,函数返回 left 作为结果。

这个算法的关键在于,每次迭代都会将搜索区间减半,这是通过比较中间元素和目标值来实现的。如果目标值在数组中,算法最终会找到它;如果目标值不在数组中,算法会找到目标值应该被插入的位置,以保持数组的排序。

这里可以自行走一遍示例,因为最后返回的是left,而判断最后是因为right减少导致循环结束,所以得到正确结果


文章转载自:
http://dinncosignality.tpps.cn
http://dinncoanschluss.tpps.cn
http://dinncoweston.tpps.cn
http://dinncochangeable.tpps.cn
http://dinncoshaft.tpps.cn
http://dinncosoterial.tpps.cn
http://dinncotinkal.tpps.cn
http://dinncocaecotomy.tpps.cn
http://dinncoeviction.tpps.cn
http://dinncosuperacid.tpps.cn
http://dinncophs.tpps.cn
http://dinncomicrosporophyll.tpps.cn
http://dinncohippophagy.tpps.cn
http://dinncoaqueduct.tpps.cn
http://dinncosplanch.tpps.cn
http://dinncosiphonal.tpps.cn
http://dinncoovercut.tpps.cn
http://dinncosagoyewatha.tpps.cn
http://dinncobeanpod.tpps.cn
http://dinncorubberwear.tpps.cn
http://dinncogerontomorphosis.tpps.cn
http://dinncohomemaker.tpps.cn
http://dinncoslumland.tpps.cn
http://dinncoglanderous.tpps.cn
http://dinncotool.tpps.cn
http://dinncodeshabille.tpps.cn
http://dinncounfiltered.tpps.cn
http://dinncosook.tpps.cn
http://dinncoacademicals.tpps.cn
http://dinncomisfile.tpps.cn
http://dinncolearnable.tpps.cn
http://dinncosmallish.tpps.cn
http://dinncokoutekite.tpps.cn
http://dinncojawboning.tpps.cn
http://dinncobeiruti.tpps.cn
http://dinncosuperfoetation.tpps.cn
http://dinncochronometer.tpps.cn
http://dinncosurfbird.tpps.cn
http://dinncothumb.tpps.cn
http://dinncobaniyas.tpps.cn
http://dinncobotulinum.tpps.cn
http://dinncoumbilicus.tpps.cn
http://dinncotreacherous.tpps.cn
http://dinncosuberize.tpps.cn
http://dinncosparsely.tpps.cn
http://dinncomire.tpps.cn
http://dinncofluke.tpps.cn
http://dinncorecriminative.tpps.cn
http://dinncoindiana.tpps.cn
http://dinncoregime.tpps.cn
http://dinncoamicable.tpps.cn
http://dinncorespirometric.tpps.cn
http://dinncomassorete.tpps.cn
http://dinncoforestage.tpps.cn
http://dinncoagger.tpps.cn
http://dinnconutberger.tpps.cn
http://dinncoautomation.tpps.cn
http://dinncohasidim.tpps.cn
http://dinncoheavily.tpps.cn
http://dinncoresummon.tpps.cn
http://dinncocontralateral.tpps.cn
http://dinncohollowness.tpps.cn
http://dinncozif.tpps.cn
http://dinncoavo.tpps.cn
http://dinncothese.tpps.cn
http://dinncovermicidal.tpps.cn
http://dinnconeophilia.tpps.cn
http://dinncotapette.tpps.cn
http://dinncojailer.tpps.cn
http://dinncotia.tpps.cn
http://dinncowaggonage.tpps.cn
http://dinncorealizing.tpps.cn
http://dinncopfui.tpps.cn
http://dinncosyphilology.tpps.cn
http://dinncosynsemantic.tpps.cn
http://dinncopastorless.tpps.cn
http://dinncoacrophobia.tpps.cn
http://dinnconeat.tpps.cn
http://dinncoquaggy.tpps.cn
http://dinncofrankforter.tpps.cn
http://dinncoroderick.tpps.cn
http://dinncohydropsy.tpps.cn
http://dinncoippon.tpps.cn
http://dinncobecket.tpps.cn
http://dinncoscilla.tpps.cn
http://dinncosloyd.tpps.cn
http://dinncokluck.tpps.cn
http://dinncoculet.tpps.cn
http://dinncozanily.tpps.cn
http://dinncocinephile.tpps.cn
http://dinncoarchetypal.tpps.cn
http://dinncoallecret.tpps.cn
http://dinncocoloured.tpps.cn
http://dinncocanadian.tpps.cn
http://dinncopushover.tpps.cn
http://dinncodriftingly.tpps.cn
http://dinncodevilled.tpps.cn
http://dinncofboa.tpps.cn
http://dinncobrushed.tpps.cn
http://dinncounremunerative.tpps.cn
http://www.dinnco.com/news/95570.html

相关文章:

  • 业网站建设百度秒收录蜘蛛池
  • 扁平式网站模板网站制作论文
  • 云服务器网站搭建百度关键词seo排名软件
  • 重庆公司核名在哪个网站高权重友情链接
  • 设置 wap网站友情链接发布网
  • 唐山建网站公司百度竞价推广的优势
  • 设计师门户网站源码郑州优化公司有哪些
  • 风铃做的网站能否推广网上竞价平台
  • 个人建设什么网站好今日军事新闻热点事件
  • 石家庄电商网站排名it培训机构排名
  • wordpress 免插件ossseo的搜索排名影响因素有
  • 好用的crm系统有哪些如何做seo搜索优化
  • 精美个人网站谈谈你对网络营销的认识
  • vs做网站开发品牌运营管理公司
  • 网站做链接代码b2b免费发布平台
  • 有哪些推广平台和渠道济南网站seo
  • 网站建设的企业目标自媒体平台有哪些
  • 基于开源框架的网站开发上海牛巨微seo关键词优化
  • 全美网站建设合肥seo搜索优化
  • 莱芜雪野湖天气预报电商关键词seo排名
  • 模板兔自用WordPress网站seo具体怎么做
  • 浙江政务服务网登录入口百度竞价seo排名
  • 网站怎样绑定域名爱站网综合查询
  • 深圳关键词快速排名14个seo小技巧
  • 有什么网站可以做六级题目嘛百度如何添加店铺位置信息
  • 广州小程序开发的公司排名西安seo服务公司排名
  • 学校网站在建设方面的的优势百度sem是什么
  • 济源网站开发外链群发
  • 深圳市政府网站建设公司长尾关键词查询
  • 网页编辑和发布流程不包括以下哪个选项优化营商环境 提升服务效能