题目

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. windows中Appium-desktop配合夜神模拟器的使用

    1.安装Android SDK并配好环境 ANDROID_HOME :C:\Users\18810\AppData\Local\Android\Sdk path:%ANDROID_HOME%\plat ...

  2. 【反演复习计划】【bzoj1011】zap-queries

    快三个月没做反演题了吧…… 感觉高一上学期学的全忘了…… 所以还得从零开始学推式子. # bzoj1011 标签(空格分隔): 未分类 --- 原题意思是求以下式子:$Ans=\sum\limits_ ...

  3. JSOI2016酱油记

    高一,第一次参加NOIP. 后悔初中没有报过名...唉,后悔也来不及了. 不知道自己一个暑假干了什么...算法没学多少,脑子倒是越来越不好使了. 过了初赛,数周后一脸茫(meng)然(bi)地去考场. ...

  4. Django admin 界面无法加载 CSS 问题解决方案(服务器 Apache 服务器)

    主要原因: 未找到存放静态文件的目录 没有访问目录的权限 基础知识: 在 CentOS 下面,Django 的静态文件存放在目录: /usr/lib/python2./site-packages/dj ...

  5. mysql数据库设计之三范式

    第一范式: 第二范式:   正解: 第三范式: 示例: 正解: BC范式: 示例: 正解:

  6. Django remedy a security issue refer dos attack

    Today the Django team is issuing multiple releases -- Django 1.4.8, Django 1.5.4, and Django 1.6 bet ...

  7. 走进Spark--云计算大数据新一代技术

    什么是Spark? 当然这里说的Spark指的是Apache Spark, Apache Spark™ is a fast and general engine for large-scale dat ...

  8. React-Native集成dva.js

    dvajs作为一个基于redux的状态管理框架,在react中的表现还是很不错的,如果我们想要在react-native应用中使用dvajs该怎么做呢? 首先安装dva-core和react-redu ...

  9. HDU 5935 Car【贪心,枚举,精度】

    Problem Description Ruins is driving a car to participating in a programming contest. As on a very t ...

  10. Vim求生

    [TOC] Vim 是从 vi 发展出来的一个文本编辑器.其代码补完.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用.和 Emacs 并列成为类 Unix 系统用户最喜欢的编辑器. —— ...