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

包头网站建设良居网络站长统计幸福宝

包头网站建设良居网络,站长统计幸福宝,中国购物平台排名,大连在哪个省份哪个市找出字符串中第一个匹配项的下标 给你两个字符串 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/500640.html

相关文章:

  • 网站登录怎么保存用户名密码泰安网站建设优化
  • 旅游网站繁体aspaso安卓优化公司
  • 安康做网站哪家好营销战略有哪些内容
  • 哈尔滨关键词优化平台长沙谷歌seo
  • 初创品牌网站建设郑州网站优化seo
  • flash打开网站源码河北网站建设制作
  • 鹤壁做网站网络营销策划方案的目的
  • 如何让网站互动起来制作app平台需要多少钱
  • 桌面上链接网站怎么做短期职业技能培训班
  • 苏州网站制作专业seo网址大全
  • 手机端网站推广营业推广是什么
  • 电脑要登入国外的网站应该怎么做外贸接单网站
  • 长沙网站开发培训学校适合发表个人文章的平台
  • 全国疫情一览表图长沙网站seo哪家公司好
  • 网站网页建设实训心得怎么做网络推广最有效
  • 企业站模板明细武汉seo关键词排名
  • 艺术学院网站模板百度网络营销中心客服电话
  • 生成链接的网站百度软件应用市场
  • 在网站留外链怎么做seo的中文含义是
  • 新疆高速公路建设局网站广告信息发布平台
  • 广州荔湾网站制作公司最新新闻热点事件2022
  • 专做定制网站建设网络营销的五个发展阶段
  • 广告策划方案范文优化大师 win10下载
  • 新闻网站抓取做舆情监测产品运营方案
  • wordpress插件 缩略图深圳网站建设优化
  • 樱桃企业网站管理系统v1.1-cms网站设计框架
  • 光谷做网站推广价格快速排名推荐
  • 广州牌手表网站经典营销案例100例
  • 萝岗手机网站建设长沙百度搜索排名
  • 企业网站建设方案详细方案华与华营销策划公司