zoukankan      html  css  js  c++  java
  • LeetCode

    Implement strStr()

    2014.2.28 02:38

    Implement strStr().

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

    Solution1:

      The first one is brute-force, reference is from source code of strstr() in <string.h>.

      Total time complexity is O(n * m). Space complexity is O(1).

    Accepted code:

     1 // 1WA, 1AC, standard Brute-Force solution.
     2 #include <cstring>
     3 using namespace std;
     4 
     5 class Solution {
     6 public:
     7     char *strStr(char *haystack, char *needle) {
     8         const char *ptr = haystack;
     9         size_t len = strlen(needle);
    10         if (len == 0) {
    11             return haystack;
    12         }
    13         while ((ptr = strchr(ptr, *needle)) != nullptr) {
    14             if (strncmp(ptr, needle, len) == 0) {
    15                 return (char *)ptr;
    16             }
    17             ++ptr;
    18         }
    19     }
    20 };

    Solution2:

      This solution uses KMP Algorithm, which runs in linear time.

      The key difference between KMP and Brute-Force is the "next" array, that defines the position to backtrack when mismatch happens.

      Here is the reference from Wikipedia of KMP Algorithm, if you'd like to learn more about it.

      Total time complexity is O(len(haystack) + len(needle)). Space complexity is O(needle).

    Accepted code:

     1 // 2CE, 1RE, 1WA, 1AC, KMP Algorithm
     2 #include <cstring>
     3 #include <vector>
     4 using namespace std;
     5 
     6 class Solution {
     7 public:
     8     char *strStr(char *haystack, char *needle) {
     9         if (haystack == nullptr || needle == nullptr) {
    10             return nullptr;
    11         }
    12         lp = strlen(needle);
    13         lw = strlen(haystack);
    14         
    15         if (lp == 0) {
    16             return haystack;
    17         }
    18         
    19         if (lw < lp) {
    20             return nullptr;
    21         }
    22         
    23         calculateNext(needle);
    24         char *result = KMPMatch(haystack, needle);
    25         next.clear();
    26         
    27         return result;
    28     }
    29     int lp;
    30 private:
    31     int lw;
    32     vector<int> next;
    33     
    34     void calculateNext(char *pat) {
    35         int i = 0;
    36         int j = -1;
    37         
    38         next.resize(lp + 1);
    39         next[0] = -1;
    40         while (i < lp) {
    41             if (j == -1 || pat[i] == pat[j]) {
    42                 ++i;
    43                 ++j;
    44                 next[i] = j;
    45             } else {
    46                 j = next[j];
    47             }
    48         }
    49     }
    50     
    51     char* KMPMatch(char *word, char *pat)
    52     {
    53         int index;
    54         int pos;
    55         
    56         index = pos = 0;
    57         while (index < lw) {
    58             if (pos == -1 || word[index] == pat[pos]) {
    59                 ++index;
    60                 ++pos;
    61             } else {
    62                 pos = next[pos];
    63             }
    64             
    65             if (pos == lp) {
    66                 // the first match is found
    67                 return word + (index - lp);
    68             }
    69         }
    70         
    71         return nullptr;
    72     }
    73 };
  • 相关阅读:
    调用控制台程序函数 RunProcess
    url传递中文的解决方案(备忘)
    Microsoft.SharePoint.SPException 安全性验证无效——错误解决
    Windows SharePoint Services 虚拟服务器没被配置为与 ASP.NET 2.0.50727.42 一起使用解决办法
    automation服务器不能创建对象
    InfoPath窗体事件列表说明和示例使用
    ORA01113:文件n需要介质恢复 (转载)
    DevExpress 第三方控件汉化的全部代码和使用方法 (转载)
    爱的感觉(转载)
    关于Oracle 01122,01110,01207的错误和解决(转载)
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3572745.html
Copyright © 2011-2022 走看看