题目

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. LeetCode 4 :Majority Element

    problem:Given an array of size n, find the majority element. The majority element is the element tha ...

  2. 中断处理函数中不用disable_irq而用disable_irq_nosync原因【转】

    转自:http://blog.csdn.net/beyondioi/article/details/9201695 今天在写触摸屏驱动时在中断处理函数中使用disable_irq关中断发现在进入中断处 ...

  3. 用maven创建第一个SpringMVC

    首先创建一个maven项目,然后依次完成如下配置: 在pom.xml加入如下基础配置,利用maven加载我们所需要的jar: <project xmlns="http://maven. ...

  4. 非旋转Treap——普通平衡树

    扔板跑…… #include<bits/stdc++.h> #define N 100010 #define mp make_pair using namespace std; typed ...

  5. 【Android开发日记】之入门篇(十三)——Android的控件解析

    Android的控件都派生自android.view.View类,在android.widget包中定义了大量的系统控件供开发者使用,开发者也可以从View类及其子类中,派生出自定义的控件. 一.An ...

  6. BZOJ 1008: [HNOI2008]越狱-快速幂/取模

    1008: [HNOI2008]越狱 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 8689  Solved: 3748 Description 监狱有 ...

  7. 高效mysql的习惯(程序员版本)

    高效的mysql 一定要有主键 如果没有主键: 数据多次读写后可能更离散,有更多随机I/O MySQL复制环境中,如果选择RBR模式,没有主键的update需要读全表,导致复制延迟 好的主键特点: 没 ...

  8. 机器人搬重物(BFS)

    机器人搬重物 时间限制: 1 Sec  内存限制: 128 MB提交: 22  解决: 10[提交][状态][讨论版] 题目描述 机 器人移动学会(RMI)现在正尝试用机器人搬运物品.机器人的形状是一 ...

  9. 进程注入后门工具Cymothoa

    进程注入后门工具Cymothoa   Cymothoa是一款隐秘的后门工具.它通过向目标主机活跃的进程注入恶意代码,从而获取和原进程相同的权限.该工具最大的优点就是不创建新的进程,不容易被发现.由于该 ...

  10. [USACO17DEC]Greedy Gift Takers

    题目描述 Farmer John's nemesis, Farmer Nhoj, has NN cows (1 \leq N \leq 10^51≤N≤105 ), conveniently numb ...