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:

  1. class Solution {
  2. public:
  3. char *strStr(char *haystack, char *needle) {
  4. int i = 0, j = 0;
  5. while(haystack[i] != '\0' && needle[j] != '\0') {
  6. if(haystack[i] == needle[j])
  7. ++i, ++j;
  8. else
  9. i = i-j+1, j = 0;
  10. }
  11. return needle[j] == '\0' ? haystack+(i-j) : NULL;
  12. }
  13. };

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] 重新开始匹配。

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

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. void getNext(char *P, vector<int> &next) {
  6. for (int i = 0; P[i] != '\0'; ++i) {
  7. if (i == 0) next.push_back(0);
  8. else next.push_back((P[i] == P[next[i-1]]) ? next[i-1]+1 : 0);
  9. }
  10. }
  11. class Solution {
  12. public:
  13. char *strStr(char *haystack, char *needle) {
  14. vector<int> next;
  15. getNext(needle, next);
  16. int i = 0, j = 0;
  17. while (haystack[i] != '\0' && needle[j] != '\0') {
  18. if (haystack[i] == needle[j]) ++i, ++j;
  19. else if (j == 0) ++i;
  20. else j = next[j-1];
  21. }
  22. return needle[j] == '\0' ? haystack+(i-j) : NULL;
  23. }
  24. };
  25.  
  26. int main() {
  27. char *T = "missiissippi", *P = "issip";
  28. cout << (Solution().strStr(T, P) ? Solution().strStr(T, P) : "NULL") << endl;
  29. return 0;
  30. }

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. CAN基础知识

    CAN:Controller Area Network,是ISO国际标准化的串行通信协议. CAN控制器根据两根线上的电位来判断总线电平.总线电平分为显性电平和隐性电平,二者必居其一.发送方通过使总线 ...

  2. ionic本质

    ionic本质一开发工具,ionic项目目录里在大堆东西,一堆插件(plugins),还有一堆npm包(node_modules),其实都是骗人的-( ̄▽ ̄-)~ 什么nodejs,npm只是为了解决 ...

  3. kvm虚拟机安装

    KVM虚拟化技术介绍 概述 KVM是基于内核的虚拟化技术(Kernel-based Virtual Machine),于2007年的Linux 2.6.20被合并进Linux内核.KVM要求CPU支持 ...

  4. python leetcode 日记 --Contains Duplicate II --219

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  5. jquery替换URL参数值

    由于经常会用到替换URL参数值,而网上写的方法代码都太长了,所以在这里写了一个简单的方法,供大家使用. 说明: reLoad(参数名,参数值) function reLoad(p, v) { var ...

  6. 用qpython3写一个最简单的发送短信的程序

    到目前为止并没有多少手机应用是用python开发的,不过qpython可以作为一个不错的玩具推荐给大家来玩. 写一个最简单的发送短信的程序,代码如下: #-*-coding:utf8;-*- #qpy ...

  7. TCP/IP、Http、Socket的区别--特别仔细

    网络由下往上分为 物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 通过初步的了解,我知道IP协议对应于网络层,TCP协议对应于传输层,而HTTP协议对应于应用层, 三者从本质上来说没有可 ...

  8. CSS中相对定位与绝对定位

    看了几个讲解定位的博客,觉得还不错,分享之: 博客一:http://blog.sina.com.cn/s/blog_4bcf4a5e010008o0.html 文章中,主要需要参考的有两点: 1,相对 ...

  9. RSA非对称加密

    先上RSA加密算法的一些简介(截图自轩辕老师的课件): 嗯--RSA就是这么一回事,于是有了如下题目: 1.In an RSA system, the public key of a given us ...

  10. (转)解决Android SDK Manager无法更新或下载太慢问题

    原帖地址:http://blog.csdn.net/exlsunshine/article/details/22208857 天朝的网络...哎~真是无语...还好最近装了谷歌的chrome浏览器+红 ...