一天一道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. ngx.ctx

    https://github.com/openresty/lua-nginx-module#ngxctx 要点 生命周期和请求一致 每个请求的ngx.ctx是相互独立的,包括ngx.location. ...

  2. 深入解读XML解析

    一.XML是什么?有什么用? XML是指.作为配置文件存在 二.XML的基本语法 1.文档声明:很重要 在编写XML文档时,需要先使用文档声明来声明XML文档.且必须出现在文档的第一行. 作用:告知解 ...

  3. iOS开发基础之开发证书的说明和发布

    1.首先通过钥匙串访问--证书助理--从证书颁发机构请求证书--填写证书信息(邮箱,常用名称,存储到磁盘)--存储为(自定义名称.certSigningReuqest,简称CSR文件,只是为了提交到苹 ...

  4. Android适配难题全面总结

    支持多种屏幕 Android 可在各种具有不同屏幕尺寸和密度的设备上运行.对于 应用,Android 系统在不同设备中提供一致的开发环境, 可以处理大多数工作,将每个应用的用户界面调整为适应其显示的 ...

  5. syslog(),closelog()与openlog()--日志操作函数

    在典型的 LINUX 安装中,/var/log/messages 包含所有的系统消息,/var/log/mail 包含来自邮件系统的其它日志消息,/var/log/debug 可能包含调试消息.根据你 ...

  6. HTML简单使用

    HTML简单使用 标签 : 前端技术 HTML HTML(Hypertext Marked Language), 即超文本标记语言,能够独立于各种操作系统平台(如UNIX/Linux/Windows等 ...

  7. J-Robot,能走、能跳舞的机器人

      最近一个月基本上没有更新博客了,主要是和朋友一起在捣鼓J-Robot这个机器人,现在基本是可以控制它了,也算是一点小小的成就感吧.   先来几张图片吧. 再来一张:   是否觉得呆呆的?来,Jim ...

  8. Java进阶(四十)Java类、变量、方法修饰符讲解

    Java进阶(四十)Java类.变量.方法修饰符讲解 Java类修饰符 abstract: 将一个类声明为抽象类,没有实现的方法,需要子类提供方法实现. final: 将一个类生命为最终(即非继承类) ...

  9. iOS 南京互联网大会分享及个人见解 韩俊强的博客

    首先分两大块: 1.如何打造高效/稳定的App (重点): 2.软件自动化测试: 每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 每日更新关注:http://we ...

  10. OpenCV特征点检测匹配图像-----添加包围盒

    最终效果: 其实这个小功能非常有用,甚至加上只有给人感觉好像人脸检测,目标检测直接成了demo了,主要代码如下: // localize the object std::vector<Point ...