一天一道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. MacOS下安装rvm的几点注意

    如果用以下链接无法下载的话: curl -sSL https://get.rvm.io | bash -s stable #或者 curl -L https://rvm.io | bash -s st ...

  2. Android makefile编写基础

    首先来看一个简单的Android makefile,这个是我上篇文章写的,重新摘出来: LOCAL_PATH:=$(call my-dir) include $(CLEAR_VARS) LOCAL_M ...

  3. TV Metro界面(仿泰捷视频TV版)源码解析

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52822499 前言:上一篇介绍了 ...

  4. Android Multimedia框架总结(十五)Camera框架之Camera2补充

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52751055 前言:监于5.0之 ...

  5. ROS新闻 Towards ROS-native drones 无人机支持方案

    PX4/Firmware:https://github.com/PX4/Firmware PXFmini An open autopilot daughter-board for the Raspbe ...

  6. Swift快速给Cocoa库内置类添加便捷初始化器

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) Cocoa中的NSShadow类默认没有我们需要的实例方法,为 ...

  7. SpringMVC实现用户登录实例

    今天分享一下SpringMVC的一个登陆小案例 准备工作 创建一个Dynamic Web Project(本人是Eclipse) 添加相关的jar包,构建路径 创建springMVC-servlet. ...

  8. svn 集成 redmine 账户验证的终极解决方案

    svn 集成 redmine 账户验证的终极解决方案 赖勇浩(http://laiyonghao.com) 动机 对于大部分开发团队来说,一般都需要一套 SCM 系统,通常是 svn + redmin ...

  9. Redefine:Change in the Changing World

    EMC World 2014的主题就是REDEFINE.的确,现在科技的发展在重新定义了技术,影响了生活,改变了你我. 对于一个有数万员工,甚至数十万员工的企业来说,Redefine无疑更加具有挑战, ...

  10. python上下文管理器ContextLib及with语句

    http://blog.csdn.net/pipisorry/article/details/50444736 with语句 with语句是从 Python 2.5 开始引入的一种与异常处理相关的功能 ...