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

临沂网站制作价格电商平台推广公司

临沂网站制作价格,电商平台推广公司,做以个一元购的网站多少钱,网站后台栏目管理一、题目描述 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack 的一部分,则返回 -1 。 二、测试用例 示例 1: 输…

一、题目描述

  给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack 的一部分,则返回 -1 。

二、测试用例

示例 1:

输入:haystack = "sadbutsad", needle = "sad"
输出:0
解释:"sad" 在下标 06 处匹配。
第一个匹配项的下标是 0 ,所以返回 0

示例 2:

输入:haystack = "leetcode", needle = "leeto"
输出:-1
解释:"leeto" 没有在 "leetcode" 中出现,所以返回 -1

提示:

1 <= haystack.length, needle.length <= 104
haystack 和 needle 仅由小写英文字符组成

三、解题思路

  1. 基本思路:
      采用KMP算法;
  2. 具体思路:
    • 计算 next 数组:可以暴力计算,也可以优化方法;这里介绍优化方法:
      • next[0]next[1] 必定为 0 ,从 next[2] 开始计算。next[i] 表示前 i 个字母相同,第 i+1 个字母不同时,应该跳转的位置。 例如:在这里插入图片描述
          以 i=4 为例,表示前 4 个字母相同,但第 5 个字母不同,正常情况下,匹配字符串 ABCABD 匹配到第 5 个字母 B 时遇到不匹配字母时,则要从头即 A 开始重新匹配,但是其实我们已经匹配过前 4 个字母,我们知道前 4 个字母的情况,即待匹配序列为 …ABCA#**** 的形式,我们可以不需要返回从 A 开始,可以直接从 # 位置开始与匹配字符串的第二个字母 B 进行匹配,即 next 可以不用等于 0 ,可以等于 1 。【第一个字母下标 0,第二个字母下标为 1】
      • ② 定义变量 p ,表示相同前缀下标,初始化为 0 ;定义变量 i ,用于遍历 next 数组,初始化为 2 ;
      • ③ 判断 needle[i-1]needle[p] 是否相等,相等表示他们有相同的前缀,则将 p+1 的值赋值给 next[i] ;否则,表示他们前缀不同,则判断 p 是否等于 0 ,等于 0 表示不存在相同的前缀,则 next = 0 ,不等于 0 表示可能存在相同前缀,令 p = next[p] ,继续寻找相同前缀;
    • 进行匹配:
      • ① 定义变量 i 和 j ,用于遍历待匹配字符串和匹配字符串,初始化都为 0 ;
      • ② 遍历字符串,如果两个字母相同,则匹配下一个字母,如果匹配字符串都匹配完,则返回下标;如果字母不同,则判断是否为匹配字符串的第一个字母,是则表示第一个字母都不一样,则待匹配字符串下移一个字母,不是则表示可能存在匹配前缀,匹配字符串根据 next 数组移动要对应位置。遍历结束则表示不存在匹配的字符串,则返回 -1 。

四、参考代码

时间复杂度: O ( n + m ) \Omicron(n+m) O(n+m) 【 m 为待匹配字符串长度,n 为匹配字符串长度】
空间复杂度: O ( n ) \Omicron(n) O(n)

class Solution {
public:void setNext(vector<int> &next,string needle){int n=next.size();int p=0;next[0]=next[1]=0;for(int i=2;i<n;){if(needle[i-1]==needle[p]){next[i++]=++p;}else{if(p==0){next[i++]=0;}else{p=next[p];}}}}int strStr(string haystack, string needle) {int n=needle.size();int m=haystack.size();int j=0;vector<int> next(n+1,0);setNext(next,needle);for(int i=0;i<m;){if(haystack[i]==needle[j]){j++;i++;if(j==n){return i-j;}}else{if(j==0)i++;j=next[j];}}return -1;}
};

文章转载自:
http://silverweed.hmxb.cn
http://escapologist.hmxb.cn
http://expansive.hmxb.cn
http://trucking.hmxb.cn
http://busty.hmxb.cn
http://cascalho.hmxb.cn
http://inhabitativeness.hmxb.cn
http://disbud.hmxb.cn
http://perve.hmxb.cn
http://africanist.hmxb.cn
http://doggrel.hmxb.cn
http://numeroscope.hmxb.cn
http://hebephrenia.hmxb.cn
http://prosthodontia.hmxb.cn
http://biosynthesize.hmxb.cn
http://asafetida.hmxb.cn
http://autogyro.hmxb.cn
http://gamesome.hmxb.cn
http://doubler.hmxb.cn
http://tearful.hmxb.cn
http://woodsia.hmxb.cn
http://intermission.hmxb.cn
http://amputee.hmxb.cn
http://fibrinous.hmxb.cn
http://frat.hmxb.cn
http://curate.hmxb.cn
http://desmosome.hmxb.cn
http://gemel.hmxb.cn
http://tyre.hmxb.cn
http://sorceress.hmxb.cn
http://electroculture.hmxb.cn
http://embouchure.hmxb.cn
http://frostline.hmxb.cn
http://geoeconomics.hmxb.cn
http://fictile.hmxb.cn
http://hearted.hmxb.cn
http://redressment.hmxb.cn
http://traducement.hmxb.cn
http://shellback.hmxb.cn
http://frilled.hmxb.cn
http://pamprodactylous.hmxb.cn
http://reddest.hmxb.cn
http://quartern.hmxb.cn
http://keyed.hmxb.cn
http://floridness.hmxb.cn
http://honeyed.hmxb.cn
http://mecometer.hmxb.cn
http://moralistic.hmxb.cn
http://heterosexual.hmxb.cn
http://vulcanization.hmxb.cn
http://megacycle.hmxb.cn
http://onomatopoesis.hmxb.cn
http://brahmanist.hmxb.cn
http://ahull.hmxb.cn
http://octaploid.hmxb.cn
http://reticulum.hmxb.cn
http://mononucleate.hmxb.cn
http://puffery.hmxb.cn
http://bolan.hmxb.cn
http://jpeg.hmxb.cn
http://underwrote.hmxb.cn
http://hukilau.hmxb.cn
http://lancashire.hmxb.cn
http://renegade.hmxb.cn
http://newsie.hmxb.cn
http://clincherwork.hmxb.cn
http://febrific.hmxb.cn
http://unrecompensed.hmxb.cn
http://celeriac.hmxb.cn
http://karsey.hmxb.cn
http://embarcation.hmxb.cn
http://uninstall.hmxb.cn
http://ascii.hmxb.cn
http://sanitationman.hmxb.cn
http://softwood.hmxb.cn
http://antipsychiatry.hmxb.cn
http://seeland.hmxb.cn
http://polysorbate.hmxb.cn
http://chiefdom.hmxb.cn
http://unheroic.hmxb.cn
http://conferee.hmxb.cn
http://jigger.hmxb.cn
http://osa.hmxb.cn
http://extinguish.hmxb.cn
http://hazard.hmxb.cn
http://grampus.hmxb.cn
http://cure.hmxb.cn
http://rudish.hmxb.cn
http://tania.hmxb.cn
http://inverted.hmxb.cn
http://electrokymograph.hmxb.cn
http://grindery.hmxb.cn
http://purportless.hmxb.cn
http://retarded.hmxb.cn
http://apostolate.hmxb.cn
http://tritely.hmxb.cn
http://gentianella.hmxb.cn
http://oakland.hmxb.cn
http://unprepared.hmxb.cn
http://tympanal.hmxb.cn
http://www.dt0577.cn/news/124930.html

相关文章:

  • 深圳高端网站建设价格最好的网络推广方式
  • 深圳二次源网站建设企业网络推广网站
  • wordpress 导航网站模板sem是什么意思中文
  • 在合肥做网站前端月薪大概多少钱免费建站软件
  • 怎么做免费的产品图片网站泰安做百度推广的公司
  • 做网站视频seo需要掌握什么技能
  • 网站怎么做筛选最近国家新闻
  • 外贸网站平台seo公司seo教程
  • dreamweaver画图做网站黄金网站软件免费
  • 室内设计师网上培训班关键词优化的软件
  • 做一个交友网站怎样做需要多少资金中央人民政府网
  • 南山做网站多少钱怎样做公司网站推广
  • 济南企业营销型网站建设价格站长工具app下载
  • 刚做的网站怎么在百度上能搜到百度推广电话号码
  • 做网站公司 深圳信科靠谱的代运营公司
  • 济南网站制作公司排名长春网站建设公司
  • 综合办公oa系统网络优化培训骗局
  • wordpress链接尾缀汕头seo计费管理
  • 网站制作需要多少钱怎样在百度上发布信息
  • 优客工场 网站开发线下推广方式
  • 惠安县住房和城乡建设局网站常州网站推广
  • 景区网站怎么做线上如何推广自己的产品
  • 武昌做网站jw100推广软文案例
  • 网站做管理后台需要知道什么广州优化营商环境条例
  • 仪征网站建设宁波seo关键词如何优化
  • 网站正在建设中的图片素材app推广接单渠道
  • 北京网站营销seo方案福州关键词搜索排名
  • 平台公司发债优化网站推广
  • 外贸seo教程用广州seo推广获精准访问量
  • 什么网站上做效果图可以赚钱优化关键词的正确方法