一天一道LeetCode系列

(一)题目

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

(二)解题

第一种解法:朴素匹配算法


/*

两个指针,分别指向两个字符串的首字符

如果相等则一起向后移动,如果不同i取第一个相同字符的下一个开始继续匹配

如果最后j等于needle的长度则匹配成功,返回i-j

否则返回0

*/

class Solution {

public:

    int strStr(string haystack, string needle) {

        int j,i;

        for(i = 0 , j =0 ; i<haystack.length() && j < needle.length() ;)

        {

            if(i+needle.length()>haystack.length()) return -1;

            if(haystack[i]==needle[j]){//如果匹配上就继续向后匹配

                i++;

                j++;

            }

            else{

                i-=j-1;//回溯到匹配开始时needle的首字符对应的下一位

                j=0;//j回溯到needle的首字符

            }

        }

        if(j==needle.length()) return i-j;

        else return -1;

    }

};

第二种解法:KMP模式匹配算法

关于kmp,请自行百度或者大话数据结构P143页


class Solution {

public:

    int strStr(string haystack, string needle) {

        int hlen = haystack.length();

        int nlen = needle.length();

        if(hlen==0) return nlen==0?0:-1;//临界值判断

        if(nlen==0) return 0;//needle为NULL,就直接返回0

        int* next = new int[nlen+1];

        getNext(needle,next);

        int i = 0;

        int j = 0;

        while(i<hlen&&j<nlen){

            if(j==-1 || haystack[i]==needle[j]){

                i++;j++;

            }

            else j=next[j];

        }

        if(j==nlen) return i-j;//等于nlen代表匹配成功,返回i-j即needle首字符在haystack中的位置

        else return -1;

    }

    void getNext(string& needle,int next[])

    {

        int i = 0;

        int j = -1;

        next[0] = -1;

        while(i<needle.length()){

            if(j==-1 || needle[i]==needle[j]){

                i++;

                j++;

                if(needle[i] == needle[j]) next[i] = next[j];//kmp优化,防止aaaaab和aaaac前四位的无效                

                else next[i] = j;

            }

            else

                j=next[j];

        }

    }

};

【一天一道LeetCode】#28. Implement strStr()的更多相关文章

  1. 44. leetcode 28. Implement strStr()

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

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

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

  3. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  4. Java [leetcode 28]Implement strStr()

    题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  5. [LeetCode] 28. Implement strStr() 解题思路

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

  6. Leetcode 28——Implement strStr()

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

  7. [leetcode]28. Implement strStr()实现strStr()

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

  8. [LeetCode] 28. Implement strStr() ☆

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

  9. LeetCode 28 Implement strStr() (实现找子串函数)

    题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description   Problem : 实现找子串的操作:如果没有找到则返回 ...

  10. LeetCode——28. Implement strStr()

    题目: class Solution { public: int strStr(string haystack, string needle) { if(needle.empty()){ return ...

随机推荐

  1. windows pe

    下载adk https://www.microsoft.com/en-us/download/details.aspx?id=30652 安装 C:\Program Files (x86)\Windo ...

  2. jdbc批量插入

    分享牛,分享牛原创.有这样一个需求,文本文件中的数据批量的插入mysql,怎么用jdbc方式批量插入呢? jdbc默认提供了批量插入的方法,可能用一次就忘记了,这里做笔记记录一下jdbc批量插入吧. ...

  3. python模块:网络协议和支持

    python模块:网络协议和支持 webbrowser 调用浏览器显示html文件 webbrowser.open('map.html') [webbrowser - Convenient Web-b ...

  4. Python动态展现之一

    首先: def f(): print('first') def g(): f() g() def f(): print('second') g() 结果: >>> first sec ...

  5. 亲密接触Redis-第一天

    引言 nosql,大规模分布式缓存遍天下,Internet的时代在中国由其走得前沿,这一切归功于我国特色的电商.因此nosql.大数据技术在中国应用的比国外还要前沿.从这一章开始我们将开始进入到真正的 ...

  6. FFmpeg与libx264接口源代码简单分析

    ===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...

  7. 5.关于QT中的网络编程,QTcpSocket,QUdpSocket

     1 新建一个项目:TCPServer.pro A  修改TCPServer.pro,注意:如果是想使用网络库,需要加上network SOURCES += \ TcpServer.cpp \ T ...

  8. MySQL 视图技术

    以前也只是知道数据库中有视图这么个概念,但是没有去深究,今天正好有时间,就来总结一下吧. 视图的定义 视图就是从一个或多个表中,导出来的表,是一个虚拟存在的表.视图就像一个窗口(数据展示的窗口),通过 ...

  9. Android首选项SharedPreference-android学习之旅(六)

    SharedPrefenence采用的键值对的方式来进行存储,采用内部存储的方式. 实例 public class MainActivity extends Activity { private Sh ...

  10. 3、Android构建仪表测试

    不同于运行于JVM的本地单元测试,仪表测试运行于你的物理设备或虚拟机中.当你需要访问设备的信息(比如Context)或者使用真正的Android framework组件时(比如SharePrefere ...