解法一: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()的更多相关文章

  1. 【Leetcode】【Easy】Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  2. 【leetcode刷题笔记】Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  3. 【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 ...

  4. 【LeetCode 8_字符串_实现】String to Integer (atoi)

    , INVALID}; int g_status; long long SubStrToInt(const char* str, bool minus) { ; : ; while (*str != ...

  5. 【LeetCode 38_字符串_算术运算】Count and Say

    string countAndSay(int n) { string res; ) return res; res = "; ) { int len = res.size(); int i, ...

  6. 【LeetCode 67_字符串_算术运算】Add Binary

    string addBinary(string a, string b) { int alen = a.size(); int blen = b.size(); ) return b; ) retur ...

  7. Leetcode中字符串总结

    本文是个人对LeetCode中字符串类型题目的总结,纯属个人感悟,若有不妥的地方,欢迎指出. 一.有关数字 1.数转换 题Interger to roman和Roman to integer这两题是罗 ...

  8. LeetCode OJ:Implement strStr()(实现子字符串查找)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  9. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

随机推荐

  1. linux系统上使用unzip命令

    最近在本地使用maven打包工程后,将工程部署到linux服务器的tomcat上,使用unzip解压工程报--->未找到命令.即该命名文件未安装,需要安装一下.安装命令如下: yum insta ...

  2. 20145335《java程序设计》第三次实验报告

    20145335郝昊<java程序设计>第三次实验报告 实验目的与要求 以结对编程的方式编写一个软件,Blog中要给出结对同学的Blog网址,可以拍照展现结对编程 情况,可以参考一下其他学 ...

  3. Spring Security核心概念介绍

    Spring Security是一个强大的java应用安全管理库,特别适合用作后台管理系统.这个库涉及的模块和概念有一定的复杂度,而大家平时学习Spring的时候也不会涉及:这里基于官方的参考文档,把 ...

  4. cogs 341:[NOI2005] 聪聪与可可

    ★★   输入文件:cchkk.in   输出文件:cchkk.out   简单对比 时间限制:1 s   内存限制:256 MB [问题描述] 在一个魔法森林里,住着一只聪明的小猫聪聪和一只可爱的小 ...

  5. CentOS7安装GNOME可视化界面

    1.首先安装X(X Window System),命令为 yum groupinstall "X Window System" 回车(注意有引号)   1CentOS Linux系 ...

  6. 详解WIFI破解-Kali篇

    转自: http://www.secbox.cn/hacker/wireless/4877.html 工具: 1:笔记本 2:USB无线网卡(必备) 3:kali系统 4:靠谱字典 第一种方法: 暴力 ...

  7. Xcode8编辑代码崩溃解决办法

    更新了Xcode8带来了一系列问题,最大的困扰就是不支持插件了,而且最关键的是一敲代码就崩溃(就是写一个字母就开始崩),在网上找了很多解决,发现是之前装的插件遗留下来的问题,将插件全部删掉就解决了,下 ...

  8. Linux后台运行命令,nohup和&的区别

    &的意思是在后台运行, 什么意思呢?  意思是说, 当你在执行 ./a.out & 的时候, 即使你用ctrl C,  那么a.out照样运行(因为对SIGINT信号免疫). 但是要注 ...

  9. mysql的空闲8小时问题

    在spring中配置数据源时,必须设定destroy-method="close"属性,以便spring容器关闭时,数据源能正常关闭. 如果数据库时mysql,如果数据源配置不当, ...

  10. Matlab绘图基础——colormap在数字图像处理及三维图形展示上的应用(分层设色)

        色图(color map)是MATLAB系统引入的概念.在MATLAB中,每个图形窗口只能有一个色图.        色图是m×3 的数值矩阵,它的每一行是RGB三元组.色图矩阵可以人为地生成 ...