LeetCode(28)题解:Implement strStr()
2024-09-08 10:37:30
https://leetcode.com/problems/implement-strstr/
题目:
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
思路:
判断一个string是否另一个string的子序列并返回位置。
naive法:遍历查找,复杂度O(mn)。
advance法还有Rabin-Karp, KMP, Boyer- Moore algorithm等。
AC代码:
class Solution {
public:
int strStr(string haystack, string needle) {\
int m=haystack.size();
int n=needle.size();
if(n== && m==)
return ;
bool flag=true;
for(int i=;i<m-n+;i++){
for(int j=;j<n;j++){
if(needle[j]!=haystack[i+j]){
flag=false;
break;
}
}
if(flag==true){
return i;
}
else
flag=true;
}
return -;
}
};
最新文章
- 网络数据包收发流程(三):e1000网卡和DMA
- LINQ to PostgreSQL Tutorial
- 2D游戏编程2--windows高级编程
- [!] Error installing AFNetworking
- (知识点)JS获取网页高度
- Strtus2框架笔记
- Begin again
- mac 卸载通过官网下载包安装的node
- linux 管道通信
- 纯小白创建第一个Node程序失败-容易忽略的一个细节
- 《从Paxos到ZooKeeper 分布式一致性原理与实践》读书笔记
- 如何用ABP框架快速完成项目(3) - 为什么要使用ABP和ABP框架简介
- 24.join算法/锁_1
- cmd关闭被占用的端口命令及教程详解
- 曲苑杂坛--DML操作中如何处理那些未提交的数据
- 动态规划刷题集python代码
- (算法)Binary Tree Max Path Sum
- 网络层-IP地址
- python学习笔记(二十九)为什么python的多线程不能利用多核CPU
- 阻塞队列之六:LinkedBlockingDeque