LeetCode(28)Implement strStr()
题目
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;
}
};
LeetCode(28)Implement strStr()的更多相关文章
- Leetcode(28)-实现strStr()
实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...
- LeetCode(28): 实现strStr()
Easy! 题目描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0 ...
- LeetCode(225) Implement Stack using Queues
题目 Implement the following operations of a stack using queues. push(x) – Push element x onto stack. ...
- LeetCode(232) Implement Queue using Stacks
题目 Implement the following operations of a queue using stacks. push(x) – Push element x to the back ...
- LeetCode(28)-Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- ajax简单后台交互-我们到底能走多远系列(28)
我们到底能走多远系列(28) 1,扯淡 单身的生活,大部分时间享受自由,小部分时间忍受寂寞. 生活有时候,其实蛮苦涩,让人难以下咽.那些用岁月积累起来的苦闷,无处宣泄,在自己的脑海里蔓延成一片片荆棘, ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(28)-系统小结
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(28)-系统小结 我们从第一节搭建框架开始直到二十七节,权限管理已经告一段落,相信很多有跟上来的园友,已经 ...
- Windows Phone开发(28):隔离存储B
原文:Windows Phone开发(28):隔离存储B 上一节我们聊了目录的操作,这一节我们继续来看看如何读写文件. 首先说一下题外话,许多朋友都在摇摆不定,三心二意,其实这样的学习态度是很不好的, ...
- leecode刷题(17)-- 实现StrStr
leecode刷题(17)-- 实现StrStr 实现StrStr 描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串 ...
随机推荐
- 详解SHOW PROCESSLIST显示哪些线程正在运行列出的状态
SHOW PROCESSLIST显示哪些线程正在运行.您也可以使用mysqladmin processlist语句得到此信息.如果您有SUPER权限,您可以看到所有线程.否则,您只能看到您自己的线程( ...
- Linux进程冻结技术【转】
转自:http://blog.csdn.net/zdy0_2004/article/details/50018843 http://www.wowotech.net/ 1 什么是进程冻结 进程冻结技术 ...
- 【LeedCode】String to integer(atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- OpenAcc社区版安装教程(Linux版)(更新版)
官方安装过程如下图所示 1.安装前 下载OpenAcc社区版 1,目前为止的最新版,平台是Linux,选择Linux x86-64. 我的服务器系统是CentOs 下载地址链接:https://www ...
- linux中直接进行系统调用和通过C库调用的示例
深入了解LINUX,这方面内容不可少,这段时间再补补.. #include <syscall.h> #include <unistd.h> #include <stdio ...
- 火狐firefox插件配合scrapy,注意tbody会导致empty
有2个常有插件,一个是xpath checker,一个是firepath(配合firebug) xpath checker是个好东西,不仅可以分析,还可以验证自己抽取的xpath是否正确 但xpath ...
- 【cocos2d-js官方文档】十七、事件分发机制
简介http://blog.csdn.net/qinning199/article/details/41951517 游戏开发中一个很重要的功能就是交互,如果没有与用户的交互,那么游戏将变成动画,而处 ...
- win7 安全模式开启声音
下载运行这个reg文件 http://files.cnblogs.com/files/bushe/Activate_Sound_in_SafeMode.rar
- 51nod 编辑距离问题(动态规划)
编辑距离问题 给定两个字符串S和T,对于T我们允许三种操作:(1) 在任意位置添加任意字符(2) 删除存在的任意字符(3) 修改任意字符 问最少操作多少次可以把字符串T变成S? 例如: S= “AB ...
- 12、Django实战第12天:课程机构列表页数据展示
今天完成的是课程机构列表页.... 1.启动服务,进入xadmin后,添加5个城市信息用作测试数据 2.添加课程机构,其中有一项要上传封面图的地方要注意 封面图上传路径是我们在models中设置好的 ...