【LeetCode 28_字符串_匹配】Implement strStr()

解法一:Brute-force
int strStr(string haystack, string needle)
{
int m = haystack.size();
int n = needle.size();
if (!n)
return ;
for (int i = ; i < m - n + ; ++i) {
int k = i, j = ;
while (j < n) {
if (needle[j] == haystack[k]) {
j++;
k++;
} else {
break;
}
}
if (j == n)
return i;
}
return -;
}
解法二:KMP
vector<int> GetNext(const string& T)
{
int len = T.size();
vector<int> next(len, );
for (int i = , k = ; i < len;) {
if (T[i] == T[k])
next[i++] = ++k;
else if (k)
k = next[k - ];
else
next[i++] = ;
}
return next;
} int strStr(string haystack, string needle)
{
int m = haystack.size();
int n = needle.size();
if (n == )
return ; vector<int> next = GetNext(needle);
for (int i = , j = ; i < m;) {
if (haystack[i] == needle[j]) {
i++;
j++;
}
if (j == n)
return i - n;
if (i < m && haystack[i] != needle[j]) {
if (j)
j = next[j - ];
else
i++;
}
}
return -;
}
【LeetCode 28_字符串_匹配】Implement strStr()的更多相关文章
- 【Leetcode】【Easy】Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【leetcode刷题笔记】Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- 【LeetCode算法-28/35】Implement strStr()/Search Insert Position
LeetCode第28题 Return the index of the first occurrence of needle in haystack, or -1 if needle is not ...
- 【LeetCode 8_字符串_实现】String to Integer (atoi)
, INVALID}; int g_status; long long SubStrToInt(const char* str, bool minus) { ; : ; while (*str != ...
- 【LeetCode 38_字符串_算术运算】Count and Say
string countAndSay(int n) { string res; ) return res; res = "; ) { int len = res.size(); int i, ...
- 【LeetCode 67_字符串_算术运算】Add Binary
string addBinary(string a, string b) { int alen = a.size(); int blen = b.size(); ) return b; ) retur ...
- Leetcode中字符串总结
本文是个人对LeetCode中字符串类型题目的总结,纯属个人感悟,若有不妥的地方,欢迎指出. 一.有关数字 1.数转换 题Interger to roman和Roman to integer这两题是罗 ...
- LeetCode OJ:Implement strStr()(实现子字符串查找)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
随机推荐
- Selenium+Python定位实例
常见的定位方式参见:http://www.cnblogs.com/ranxf/p/7928732.html 1.ID定位(find_element_by_id) <input class=&qu ...
- Python实例1—格式化输出
老男孩教学学习笔记: 实例1:格式化输出 # Author:Alex Li name = input("name:") # raw_input 2.x input 3.x # in ...
- 安卓Android第三方登录-QQ登录
要实现QQ第三方登录,其实只需要一个封装类:QQLoginManager 几乎 三行代码 就实现QQ登录功能 这里先给出Github开源项目地址,项目下有详细的使用说明 下面就开始详细说一说怎么实 ...
- 安装centos7最小化安装
分享一篇不错的文章: https://www.howtoforge.com/tutorial/centos-7-server/
- 强大的jQuery选择器 平时用的太少了 下次要先来看看
- 混合开发的大趋势之一React Native手势行为那些事
转载请注明出处:王亟亟的大牛之路 最近项目部分模块重构,事情有点多,学习进度有所延缓,外加一直在吸毒(wow你懂的),导致好多天没发问了,其实这部分知识月头就想写了,一直没补. 话不多说先安利:htt ...
- kotlin 记录(已弃坑)
kotlin 有些是转载内容 使用nullable值以及空值检测 引用或函数返回值如果可能为null值,则必须显式标记nullable. (在类型后面跟一个问号表示这个对象可能为空,跟两个感叹号表示这 ...
- Build hadoop 2.5.2 with Java8
mvn clean package -Pdist,native -DskipTests -Dtar -Dmaven.javadoc.skip=true
- java23种设计模式之三: 适配器模式
一.适配器模式 就是个通过一个中间件转化,可以将不匹配的两件事整合到一起,把不匹配变的匹配. 二.适配器分类 1.类适配器 2.对象适配器 三. 适配器的3种组成 1.类适配器组成 1.2个接口 ...
- javaScript tips —— z-index 对事件机制的影响
demo // DOM结构 class App extends React.Component { componentDidMount() { const div1 = document.getEle ...