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

网站的开发与建设项目新网站怎么快速收录

网站的开发与建设项目,新网站怎么快速收录,如何制作wordpress主题,西宁网站搭建专业公司找出字符串中第一个匹配项的下标 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack 的一部分,则返回 -1 。 示例 1: …

找出字符串中第一个匹配项的下标

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

示例 1:
输入:haystack = “sadbutsad”, needle = “sad”
输出:0
解释:“sad” 在下标 0 和 6 处匹配。
第一个匹配项的下标是 0 ,所以返回 0 。

方法一、暴力法

类似双指针的思想,遍历解决。优势就是简单容易理解,但时间复杂度不是最优。

复杂度
时间复杂度:O(n*m)
空间复杂度:O(1)

Swift

func strStr(_ haystack: String, _ needle: String) -> Int {let cntL = haystack.countlet cntR = needle.countvar i = 0while i+cntR <= cntL {var flag = truefor j in 0..<cntR {if haystack[haystack.index(haystack.startIndex, offsetBy: i+j)] != needle[needle.index(needle.startIndex, offsetBy: j)] {flag = falsebreak}}if flag {return i}i += 1}return -1}

OC

-(NSInteger)strStr:(NSString *)haystack needle:(NSString *)needle {if (needle.length <= 0) {return -1;}NSInteger n = haystack.length, m = needle.length;for (NSInteger i=0; i+m<=n; i++) {NSInteger j = 0;while (j<m && [haystack characterAtIndex:i+j] == [needle characterAtIndex:j]) {j++;}if (j == m) {return i;}}return -1;
}

方法二、KMP算法(推荐)

面试的时候如果能撸出来,那么基本就稳了,相反,如果只知道暴力法,那可能结果就是回去等通知吧。
具体算法讲解请参考:
彻底搞懂KMP算法!!(配视频讲解)

Swift

//KMP 解法func strStr(_ haystack: String, _ needle: String) -> Int {let n = haystack.countlet m = needle.countif m == 0 {return 0}//构造next数组var pi = Array(repeating: 0, count: m+1)var j = 0for i in 1..<m {while j > 0 && needle[needle.index(needle.startIndex, offsetBy: i)] != needle[needle.index(needle.startIndex, offsetBy:j)] {j = pi[j-1]}if needle[needle.index(needle.startIndex, offsetBy: i)] == needle[needle.index(needle.startIndex, offsetBy: j)]  {j += 1}pi[i] = j}j = 0for i in 0..<n {while j>0 && haystack[haystack.index(haystack.startIndex, offsetBy: i)] != needle[needle.index(needle.startIndex, offsetBy: j)] {j = pi[j-1]}if haystack[haystack.index(haystack.startIndex, offsetBy: i)] == needle[needle.index(needle.startIndex, offsetBy: j)] {j += 1}if j == m {return i-m+1}}return -1}

OC

//KMP
-(NSInteger)strStr:(NSString *)haystack needle:(NSString *)needle {NSInteger n = haystack.length;NSInteger m = needle.length;if (m <= 0) {return 0;}NSMutableArray *next = [NSMutableArray array];for (NSInteger i=0; i<m; i++) {next[i] = @(0);}//求next数组NSInteger i=1;NSInteger j = 0;for (; i<m; i++) {//不相等的情况if (j>0 && [needle characterAtIndex:i] != [needle characterAtIndex:j]) {j = [next[j-1] integerValue];}//相等情况if ([needle characterAtIndex:i] == [needle characterAtIndex:j]) {j++;}//赋值next[i] = [NSNumber numberWithInteger:j];}j = 0;for (i=0; i<n; i++) {if (j > 0 && [haystack characterAtIndex:i] != [needle characterAtIndex:j]) {j = [next[j-1] integerValue];}if ([haystack characterAtIndex:i] == [needle characterAtIndex:j]) {j++;}if (j == m) {return i-m+1;}}return -1;
}
http://www.dtcms.com/wzjs/255544.html

相关文章:

  • 刷leetcode对网站开发有用吗石嘴山网站seo
  • 个人域名可以做KTV网站吗深圳seo优化
  • 成都企业做网站网络营销有哪些推广方式
  • 住房和建设部网站网站快速收录教程
  • wordpress怎么二次开湖北百度seo
  • seo投放营销青岛网站seo诊断
  • 机构网站建设需要交费吗武汉seo网站推广
  • 荆州seo技术厂家谷歌优化
  • 设计小程序多少钱win7优化大师官方网站
  • 搜索视频 网站开发seo的目的是什么
  • 网站免费正能量软件苹果版小蝌蚪幸福宝入口导航
  • 网站建设验收使用情况seo计费怎么刷关键词的
  • 宿州网站建设时间蜘蛛搜索引擎
  • 网站制作素材广州seo网站推广平台
  • 微信网站建设价格热搜榜百度
  • 做纪录片卖给视频网站注册百度推广账号
  • 网站建设内部下单流程图代写软文费用全网天下实惠
  • 家庭宽带怎么做网站广东短视频seo搜索哪家好
  • 做国际网站怎么做长清区seo网络优化软件
  • 什么做网站推广广州网站优化页面
  • 网站流量数据分析怎么做培训
  • 凡科企业网站如何建设阿里域名注册官网
  • 兼职做网站安全么甘肃seo技术
  • 网站导航设计模板源码chatgpt 网址
  • 厚街镇网站仿做做小程序公司哪家好
  • 建立b2c网站网络营销的发展前景
  • 网站设计的难点seo指的是搜索引擎营销
  • 西安网站制作公司花禾科技永久免费自助建站平台
  • 网站开发和网站运营nba湖人最新新闻
  • 重庆家居网站制作公司制作网页的步骤