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

网站的开发与建设项目洛阳seo网络推广

网站的开发与建设项目,洛阳seo网络推广,wordpress页面模板,html设计软件找出字符串中第一个匹配项的下标 给你两个字符串 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/31669.html

相关文章:

  • 成都哪里有网络营销活动无锡seo关键词排名
  • 企业管理咨询服务合同范本优化大师电脑版官网
  • 网站建设静态代码网站前期推广
  • 如何做网站美工的乌鲁木齐seo
  • 做网站 上海重庆网页搜索排名提升
  • 深圳代做网站后台seo搜索引擎优化方案
  • 中小公司做网站十大搜索引擎
  • 古云网站建设自己的网站
  • 网站开发前端后端seo咨询师
  • 安源网站建设推广app最快的方法
  • 网站诊断与检测宁波seo外包服务
  • 上海jsp网站建设搜索关键词站长工具
  • asp access 手机站 用于做微网站开鲁网站seo站长工具
  • 不需要备案如何做网站百度扫一扫识别图片在线
  • 谷歌网站推广公司seminar什么意思中文
  • 机械公司网站建设百度广告投放价格表
  • 广东省建设安全卡查询网站泰安做百度推广的公司
  • crm系统是什么seo优化与推广招聘
  • 企业网站设计要点如何提高网站的自然排名
  • 邢台网站设计怎么做seo搜索优化公司
  • 金华英文网站建设官方百度app下载
  • 东莞优化网站建设网站大全软件下载
  • 网站文章百度不收录徐州seo排名收费
  • 无锡网站建设哪家做的比较好网络营销软文范例300
  • dede企业网站电商运营推广
  • 联合智慧旅游建设旅游门户网站广东疫情最新数据
  • 部门网站建设情况汇报培训学校管理制度大全
  • 网站建设交流推广seo推广沧州公司电话
  • 建设微商城网站软文文案
  • 做可视化图表的网站google下载安卓版下载