[Leetcode] implement strStr() (C++)
2024-10-12 21:44:50
Github leetcode 我的解题仓库 https://github.com/interviewcoder/leetcode
题目:
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Update (2014-11-02):
The signature of the function had been updated to return the index instead of the pointer. If you still see your function signature returns a char *
or String
, please click the reload button to reset your code definition.
Tag:
String; Two Pointers
体会:
这道题是字符串的匹配问题,目前还只会朴素的逐步方法,但是这道题还有KMP解法,和Boyer-Moore的解法,目前还没有掌握。
先把笔记放到这里,以后再来解决吧。
1. KMP算法
class Solution {
public:
int strStr(char *haystack, char *needle) {
int i = ; // index under examine in haystack
int j = ; // index under examine in needle while (haystack[i] && needle[j]) {
if (haystack[i] == needle[j]) {
i++;
j++;
} else {
// roll back:
// reset haystack index back to (i - j + 1), since in current examine
// start index in haystack is (i - j)
i = i - j + ;
// reset needle back to its beginning
j = ;
}
}
// if haystack ends but needle not ends,
// we know needle is not in the haystack, then return -1.
return (needle[j]) ? - : (i - j);
}
};
最新文章
- C# 6.0 功能预览 (一)
- hihoCoder 后缀自动机三·重复旋律6
- JDK自带工具列表
- 安装php-amqplib(RabbitMQ的phpAPI)
- HBase 实战(2)--时间序列检索和面检索的应用场景实战
- Uncaught ReferenceError: WebForm_DoPostBackWithOptions is not defined
- JavaScript基础18——js的Array对象
- XproerIM V1,2,12,65376 发布。
- 技术英文单词贴--G
- Java开发者值得关注的7款新工具
- 用VBA计算WPS 表格ET EXCEL中的行数和列数的多重方法
- 安装Oracle11g时,检测到系统的主 IP 地址是 DHCP 分配的地址
- webservice发布接口
- JQuery获取元素宽度.width()与.css(‘width’)两个函数的区别
- [Codeforces Round #192 (Div. 2)] D. Biridian Forest
- 简单理清一下proto与prototype
- Delphi引用C对象文件
- TabBar自定义方式(一)
- 用图来教你怎样用Photoshop蓝底转换红底
- QLayout窗口布局
热门文章
- 3.class文件基本结构
- include,include_once,require,require_once的区别
- Request对象 --web浏览器向web服务端的请求
- DPI情况下处理
- Ruby中的Symbol与字符串
- Android android.support.v7.appcompat.R$styleable
- 自定义DTD(myeclipser的XML提示功能)
- Oracle中对列加密的方法
- bzoj1151
- TCP/IP 中文译名为传输控制协议/因特网互联协议,又叫网络通讯协议