LeetCode OJ:Implement strStr()(实现子字符串查找)
2024-08-24 14:06:49
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
额,当然我用的就是暴力搜索来,没用到KMP之类的算法,有时间再来补上辣,代码如下:
class Solution {
public:
int strStr(string haystack, string needle) {
if(!needle.size()) return ;
if(haystack.size() < needle.size()) return -;
for(int i = ; i <= haystack.size() - needle.size(); ++i){
int start = i;
for(int j = ; j < needle.size() && start < haystack.size() && haystack[start] == needle[j]; ++j, ++start){
}
if(start-i == needle.size()) return i;
}
return -;
}
};
最新文章
- flask-whooshalchemy需要注意的一点
- brew
- Java-计划存储
- [moka同学代码]PHP初级知识:上传文件源码
- C#:控制WinForm界面的显示
- 转载 网页打印时设置A4大小
- DVRF:路由器漏洞练习靶机 Damn Vulnerable Router Firmware
- poj2367 拓扑序
- JAVA:借用OpenOffice将上传的Word文档转换成Html格式
- 基于jQuery带图标的多级下拉菜单
- C++服务器设计(六):设备连接的生命周期管理
- C#反射 入门学习 01
- MySQL服务相关
- 腾讯云中ssL证书的配置安装
- 生成ssh公钥
- 洛谷3084 [USACO13OPEN]照片Photo
- Flask初级(三)flash使用模板
- Luogu 3245 大数
- Caused by: java.lang.IllegalStateException: Expected raw type form of org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$Match
- 关于strip(切割)和 split(分开) 的区别