Implement strStr()

Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

MY: Question.

思路: 逐步查找。当出现不同时,如何回溯是关键。

Solution A:

class Solution {
public:
char *strStr(char *haystack, char *needle) {
int i = 0, j = 0;
while(haystack[i] != '\0' && needle[j] != '\0') {
if(haystack[i] == needle[j])
++i, ++j;
else
i = i-j+1, j = 0;
}
return needle[j] == '\0' ? haystack+(i-j) : NULL;
}
};

Solution B 与经典 KMP 算法:

对模式串 P 设置回溯数组 next. (next 只有模式串 P 的特性有关系,与目标串没有关系。)

next 的求法:

next[0] = 0; (0 位置最后匹配,下次还从此位置开始匹配(舍去从-1开始,没有意义))

next[pos] = (P[next[pos-1]] == P[pos] ? next[pos-1]+1 : 0);

(若回溯后的字符与当前字符相同,则应设置回溯位置在前回溯位置之后。否则,设置回溯位置为0)

模式串中:

当前位置 pos 不能匹配时, 回溯到 next[pos-1] 重新开始匹配。

当前位置匹配,则继续下去。

#include <iostream>
#include <vector>
using namespace std; void getNext(char *P, vector<int> &next) {
for (int i = 0; P[i] != '\0'; ++i) {
if (i == 0) next.push_back(0);
else next.push_back((P[i] == P[next[i-1]]) ? next[i-1]+1 : 0);
}
}
class Solution {
public:
char *strStr(char *haystack, char *needle) {
vector<int> next;
getNext(needle, next);
int i = 0, j = 0;
while (haystack[i] != '\0' && needle[j] != '\0') {
if (haystack[i] == needle[j]) ++i, ++j;
else if (j == 0) ++i;
else j = next[j-1];
}
return needle[j] == '\0' ? haystack+(i-j) : NULL;
}
}; int main() {
char *T = "missiissippi", *P = "issip";
cout << (Solution().strStr(T, P) ? Solution().strStr(T, P) : "NULL") << endl;
return 0;
}

70. Implement strStr() 与 KMP算法的更多相关文章

  1. LeetCode Implement strStr()(Sunday算法)

    LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...

  2. 第3章:LeetCode--算法:strStr KMP算法

    https://leetcode.com/problems/implement-strstr/  28. Implement strStr() 暴力算法: int ViolentMatch(char* ...

  3. 用KMP算法实现strStr()

    strStr()函数的用途是在一个字符串S中寻找某个字串P第一次出现的位置.并返回其下标,找不到时返回-1.最简单的办法就是找出S全部的子串和P进行比較,然而这种方法比較低效.假设我们从S的下标0和P ...

  4. Linux GCC下strstr的实现以及一个简单的Kmp算法的接口

    今天做了一道题,要用判断一个字符串是否是另一个字符串的子串,于是查了一下strstr的实现. 代码如下: char *strstr(const char*s1,const char*s2) { con ...

  5. 自己对kmp算法的理解,借由 28. 实现 strStr() 为例

    做题思路 or 感想 : 就借由这道题来理解一下kmp算法吧 kmp算法的操作过程我觉得有句话很合适 :KMP 算法永不回退 目标字符串 的指针 i,不走回头路(不会重复扫描 目标字符串),而是借助 ...

  6. 28. Implement strStr()(KMP字符串匹配算法)

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  7. [leetcode 27]Implement strStr()

    1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  8. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  9. Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

随机推荐

  1. web前端基础篇⑥

    LESS.①是一种拓展技术,基于css.②包含变量.混合.函数.运算.③简化css代码.降低维护成本④目前用的解析器(koala) 变量(值可变)@变量名:值步骤:①建立文件夹②建html和less两 ...

  2. Android常见控件— — —AlertDialog

    package com.example.uiwidgettest2; import android.app.Activity;import android.app.AlertDialog;import ...

  3. layoutSubviews总结

    ios layout机制相关方法 - (CGSize)sizeThatFits:(CGSize)size - (void)sizeToFit ——————- - (void)layoutSubview ...

  4. LR接口测试手工脚本与验证脚本

    Action(){ char URL[250]; char mars_cid[30]; memset(URL,0,sizeof(URL)); memset(mars_cid,0,sizeof(mars ...

  5. C语言之共用体

    最近在复习C语言,特再次记录: 有时需要将几种不同类型的变量存放到同一段内存单元中.如一个int.float.char放在同一个地址开始的内存单元中.,也就是使用覆盖技术,几个变量相互覆盖,称之为共用 ...

  6. Asp.Net异步导入Excel

    故事:用户在页面上传一个excel文件,程序把excel里的内容入库. 技术方案:保存文件在服务器,jquey Ajax 异步读取文件中的记录到数据库,在页面实时刷新导入情况 页面前端 <%@ ...

  7. Call to undefined function curl_init()

    运行PHP不支持curl_init()的解决方法: 1.修改php.ini,将;extension=php_curl.dll前面的分号去掉(同时检查扩展的引用路径是否正确)2.拷贝libeay32.d ...

  8. Android 学习第4课,一些小知识

    java语言的注释有如下3种: 1. // 2./* 注释内容 */ 3./**     注释内容    */       这种叫文档注释,这种注释常被javaDoc文档工具读取作为 JavaDoc文 ...

  9. 解决 xx.h has been modified since the precompiled header 系统头文件被修改

    fatal error: file '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Dev ...

  10. PHP面向对象(分页)

    <?php   class page   {      var $table;      var $pagesize;      var $totalsize;      var $totalp ...