题目

Implement strStr().

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

Update (2014-11-02):

The signature of the function had been updated to return the index instead of the pointer. If you still see your function signature returns a char * or String, please click the reload button to reset your code definition.

分析

这是一道模式匹配算法。给定两个字符串haystack与needle,给出needle在haystack全然匹配的首位置坐标(从0開始)。

这道题在数据结构中有解说,除了開始的简单模式匹配算法BF算法,还给出了改进后的KMP经典算法。以下分别用这两个算法实现。

AC代码

class Solution {
public:
//简单模式匹配算法(BF算法)
int strStr(string haystack, string needle) {
int len = strlen(haystack.c_str()), nl = strlen(needle.c_str()); int i = 0, j = 0;
while (i < len && j < nl)
{
if (haystack[i] == needle[j])
{
i++;
j++;
}
else{
i = i - j + 1;
j = 0;
}//else
}//while if (j == nl)
return i - nl;
else
return -1;
}
};

KMP算法实现

class Solution {
public:
//简单模式匹配算法(BF算法)
int strStr(string haystack, string needle) {
int len = strlen(haystack.c_str()), nl = strlen(needle.c_str()); int i = 0, j = 0;
while (i < len && j < nl)
{
if (haystack[i] == needle[j])
{
i++;
j++;
}
else{
i = i - j + 1;
j = 0;
}//else
}//while if (j == nl)
return i - nl;
else
return -1;
} //从字符串haystack的第pos个位置開始匹配
int KMP(const string &haystack, const string &needle, int pos)
{
int len = strlen(haystack.c_str()), nl = strlen(needle.c_str()); int i = pos, j = 0;
int *n = Next(needle);
while (i < len && j < nl)
{
if (j == -1 || haystack[i] == needle[j])
{
i++;
j++;
}
else{
j = n[j];
}//else
}//while if (j == nl)
return i - nl;
else
return -1; } int* Next(const string &s)
{
int i = 0, j = -1;
int next[500] ;
int len = strlen(s.c_str());
next[0] = -1;;
while (i < len)
{
while (j >-1 && s[i] != s[j])
j = next[j];
i++;
j++; if (s[i] == s[j])
next[i] = next[j];
else
next[i] = j;
}//while
return next;
}
};

GitHub測试程序源代码

LeetCode(28)Implement strStr()的更多相关文章

  1. Leetcode(28)-实现strStr()

    实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...

  2. LeetCode(28): 实现strStr()

    Easy! 题目描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0 ...

  3. LeetCode(225) Implement Stack using Queues

    题目 Implement the following operations of a stack using queues. push(x) – Push element x onto stack. ...

  4. LeetCode(232) Implement Queue using Stacks

    题目 Implement the following operations of a queue using stacks. push(x) – Push element x to the back ...

  5. LeetCode(28)-Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  6. ajax简单后台交互-我们到底能走多远系列(28)

    我们到底能走多远系列(28) 1,扯淡 单身的生活,大部分时间享受自由,小部分时间忍受寂寞. 生活有时候,其实蛮苦涩,让人难以下咽.那些用岁月积累起来的苦闷,无处宣泄,在自己的脑海里蔓延成一片片荆棘, ...

  7. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(28)-系统小结

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(28)-系统小结 我们从第一节搭建框架开始直到二十七节,权限管理已经告一段落,相信很多有跟上来的园友,已经 ...

  8. Windows Phone开发(28):隔离存储B

    原文:Windows Phone开发(28):隔离存储B 上一节我们聊了目录的操作,这一节我们继续来看看如何读写文件. 首先说一下题外话,许多朋友都在摇摆不定,三心二意,其实这样的学习态度是很不好的, ...

  9. leecode刷题(17)-- 实现StrStr

    leecode刷题(17)-- 实现StrStr 实现StrStr 描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串 ...

随机推荐

  1. python实战===石头剪刀布,简单模型

    #石头剪刀布 import random import time win_list = [("石头","剪刀"),("布","石头 ...

  2. css3带你实现酷炫效果

    css3 私有前缀 -webkit- chrome/safari等webkit内核浏览器 -moz- firfox -o- opera -ms- IE css3 盒子模型 box-sizing 值co ...

  3. JavaScript ES6部分语法

    ES6是JavaScript语言的新版本,它也可以叫做ES2015,之前学习的JavaScript属于ES5,ES6在它的基础上增加了一些语法,ES6是未来JavaScript的趋势,而且vue组件开 ...

  4. Spring Boot 项目部署到本地Tomcat,出现访问路径问题

    首先确定war 包没问题,把war包放在webapps目录下,访问http://localhost:8080/ + 项目名称 发现首页可以访问但css,js请求都是404,跳转页面也是404 解决方法 ...

  5. 深入理解Java的注解(Annotation):注解处理器(3)

    如果没有用来读取注解的方法和工作,那么注解也就不会比注释更有用处了.使用注解的过程中,很重要的一部分就是创建于使用注解处理器.Java SE5扩展了反射机制的API,以帮助程序员快速的构造自定义注解处 ...

  6. activiti-ui源码构建

    修改数据库链接:

  7. Liquibase 快速开始

    Step 1 :创建Changelog文件,所有的数据库变动都会保存在Changelog文件中 <?xml version="1.0" encoding="UTF- ...

  8. poj 1654(利用叉积求面积)

    Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17937   Accepted: 4957 Description ...

  9. yii2中判断值是否存在二维数组中

    //在yii2中,在类里面的函数,可以不加action $arr = array( array('a', 'b'), array('c', 'd') ); in_array('a', $arr); / ...

  10. [BZOJ1076][SCOI2008]奖励关 状压dp

    1076: [SCOI2008]奖励关 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3070  Solved: 1595[Submit][Statu ...