【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 ...
随机推荐
- SQL优化,解决系统运行效率瓶颈
http://www.cnblogs.com/SameZhao/p/4737725.html
- saltstack常用模块
介绍一些常用的saltstack模块,更多模块参考官方网站 1.跟安装包相关的模块:salt.states.pkg salt.states.pkg.downloaded(name, version=N ...
- 配置linux使用mail发送邮件到163邮箱
1.进行配置 yum install -y mailx /etc/mail.rc添加对163的授权: ##########config 163 mail############set from=jso ...
- 【前端】javaScript 常用技巧总结
javaScript 常用技巧总结 1. 彻底屏蔽鼠标右键 oncontextmenu="window.event.returnValue=false" <table b ...
- python-集合、字典
文件操作: open() read() readline() readlines() write() writelines() flush() #刷新缓冲区 seek() 修改文件读写指针 t ...
- Metasploit安装——centos6.5
1:安装ruby yum -y install ruby 2:根据官方文档执行,安装支持包 yum install xorg-x11-server-Xvfb -y 3:官网下载最新版的Metaspl ...
- [CF960F]Pathwalks
题目大意:给你一张$n$个点$m$条边的带权有向图,可能有重边和自环.边会按照顺序给出.让你求出一条最长的路径,使得路径上的边满足边权和出现的时间严格递增.路径可以重复经过同一个点. 想办法把它转化成 ...
- LeetCode——Longest Palindromic Subsequence
1. Question Given a string s, find the longest palindromic subsequence's length in s. You may assume ...
- spark(四)yarn上的运行模式
架构图 yarn-cluster yarn-client 区别 Yarn-cluster spark的driver运行在applicationMaster内,启动流程为: 这张图可能比较直观 Yarn ...
- jQuery编程规范与最佳实践(附带一些个人的笔记)
加载jQuery-Loading jQuery 1.坚持使用CDN来加载jQuery,这种别人服务器免费帮你托管文件的便宜干嘛不占呢.点击查看使用CDN的好处,点此查看一些主流的jQuery CDN地 ...