• [Leetcode 83] 28 Implement strStr()


    Problem:

    Implement strStr().

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

    Analysis:

    There are two ways: one is the naive comparing algorithm and the other is KMP algorithm.

    For KMP algorithm, please refer the wiki page

    For the naive algorithm, we start from the haystack's first position until the one plus needle's length is greater than the haystack's length.

    For each position, we compare it with the needle. If they are the same, we compare the next position and repeat this process. If they are not the same, we know this i-position won't give a match. So we just break out of the loop and go to the next i position. If we reach the end of the needle position, then there is a match. And return the i. This algoritm is of complexity O(m*n)

     A special case is that the given needle is of length 0. In this case, we directly return the haystack.

    Code:

    Naive algorithm:

     1 class Solution {
     2 public:
     3     char *strStr(char *haystack, char *needle) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         int hlen = strlen(haystack);
     7         int nlen = strlen(needle);
     8         
     9         for (int i=0; i+nlen <= hlen; i++) {
    10             int j = 0;
    11             
    12             for (j=0; j<nlen; j++) {
    13                 if (haystack[j+i] != needle[j])
    14                     break;
    15             }
    16             
    17             if (j == nlen) return haystack+i;
    18         }
    19         
    20         return NULL;
    21     }
    22 };
    View Code

    KMP algorithm:

     1 class Solution {
     2 public:
     3     char *strStr(char *haystack, char *needle) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         int n = strlen(haystack), m = strlen(needle);
     7         
     8         if (m == 0)
     9             return haystack;
    10         
    11         failure_function(needle);
    12         int i = 0, j = 0;
    13         while (true) {
    14             if (i == n)
    15                 break;
    16                 
    17             if (haystack[i] == needle[j]) {
    18                 i++;
    19                 j++;
    20                 if (j == m) 
    21                     return haystack + i - j;
    22             } else if (j > 0) {
    23                 j = ff[j];
    24             } else
    25                 i++;
    26         }
    27         
    28         return NULL;
    29     }
    30     
    31     
    32     void failure_function(char *s) {
    33         int n = strlen(s);
    34         
    35         ff = new int[n+1];
    36         
    37         ff[0] = ff[1] = 0;
    38         
    39         for (int i=2; i<=n; i++) {
    40             int j = ff[i-1];
    41             while (true) {
    42                 if (s[j] == s[i-1]) {
    43                     ff[i] = j + 1;
    44                     break;
    45                 }
    46                 
    47                 if (j == 0) {
    48                     ff[i] = 0;
    49                     break;
    50                 }
    51                      
    52                 j = ff[j];
    53             }
    54         }
    55     }
    56     
    57 private:
    58     int *ff;
    59 };
    View Code
  • 相关阅读:
    span i s等行内元素标签之间出现奇怪空格符号
    使用electron搭建桌面app的简便方法
    antd card 组件实现鼠标移入移出效果
    事物处理
    最近在做支付宝支付,在本地测试一切正常,上传到服务器就遇到报错:
    tp框架报错 Namespace declaration statement has to be the very first statement in the script
    前后端分离开发,跨域访问的apche设置
    thinkphp 同一字段不同查询条件实现
    Redis-server在windows下闪退
    tp volist需要便利两个数组时的处理办法
  • 原文地址:https://www.cnblogs.com/freeneng/p/3209805.html
Copyright © 2020-2023  润新知