解法一: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. vue下载文件

    import fileDownload from 'js-file-download' let params = { ", ", "filename":&quo ...

  2. python3_Logging模块详解

    python的logging模块提供了通用的日志系统,可以方便第三方模块或应用使用. 简单使用 import logging # logging.config.fileConfig("./l ...

  3. ffmpeg下载安装和简单应用

    先介绍一下ffmpeg:FFmpeg是一个自由软件,可以运行音频和视频多种格式的录影.转换.流功能,包含了libavcodec —这是一个用于多个项目中音频和视频的解码器库,以及libavformat ...

  4. centos6.9 升级glibc(升级到 2.17版)

    原系统centos6.9自带GLIBC_2.12,安装一些软体提示版本不对,决定升级. wget http://ftp.gnu.org/gnu/glibc/glibc-2.17.tar.gz tar ...

  5. 有道云笔记配合MPic+七牛云 自制MarkDown文档图床(适用Typora)

    注:从有道云笔记v6.5开始,有道云笔记会员可以使用MarkDown有道自带的图床.(但是非会员可以采用下面的七牛云图床+MarkDown方法) 0x00 前言 一直用有道云笔记,粘贴图片,做笔记没问 ...

  6. STC12C系列单片机PWM脉宽调制

    最近给别人做了一个小东西,MCU选的是STC12C5A60S2 ,需要用到PWM控制功能. 在网上找了一下,发现解释的不尽人意,无奈之下自己琢磨数据手册弄明白了. 首先,STC12C5A60S2内置有 ...

  7. 20145307第八周JAVA学习报告

    20145307<Java程序设计>第8周学习总结 教材学习内容总结 通用API 日志API 1.java.util.logging包提供了日志功能相关类与接口,使用日志的起点是logge ...

  8. 关于office word 应用程序下载配置

    Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} fai ...

  9. elasticsearch系列(七)java定义score

    概述 ES支持groovy 和 java两种语言自定义score的计算方法,groovy甚至可以嵌套在请求的参数中,有点厉害,不过不在本篇讨论范围. 如何用自定义的java代码来定义score如何产生 ...

  10. [OpenCV]OpenCV常用语法函数与坑点

    目录 1. 加载图像(cv::imread) 2. 显示图像(cv::nameWindows与cv::imshow) 3. 修改图像(cv::cvtColor) 4. 保存图像(cv::imwrite ...