LeetCode题解:Implement strStr()

Implement strStr()

Implement strStr().

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

思路:

首先想到的是KMP算法。不过似乎不是很成功,提交了之后总是报Runtime error,但是我这里无论本地机器跑还是在valgrind上跑都不报错。奇怪的问题。

题解:

const char* strStr (const char* S, const char* W)
{
    int len_W;
    int* T; // table
    int i, j, m;
    
    if (S == NULL || W == NULL)
        return NULL;
        
    len_W = strlen (W);
    
    if (len_W == 0)
        return 0;
    else if (len_W == 1)
    {
        while (*S != 0 && *S != *W)
            ++S;
        return *S == 0 ? NULL : S;
    }
    
    // build the table
    T = (int*) malloc (len_W * sizeof (int));
    T[0] = -1;  // must forward!!
    T[1] = 0;
    
    i = 2;
    j = 0;
    while (i < len_W)
        if (W[i - 1] == W[j])
            ++j, T[i] = j, ++i;
        else if (j > 0)
            j = T[j];
        else
            ++j, ++i;
            
    // KMP
    m = 0, i = 0;
    while (S[m + i] != 0)
    {
        if (W[i] == S[m + i])
        {
            if (i == len_W - 1)
                break;
            ++i;
        }
        else
            // fall back
            m = m + i - T[i], i = T[i] > -1 ? T[i] : 0;
    }
    
    free (T);
    
    if (S[m + i] == 0)
        return NULL;
    else
        return S + m;
}
思路:

重新测试一下,用最简单的暴力法。结果超时了。

题解:

class Solution {
public:
    char* strStr (char* s, char* w)
    {
        if (s == nullptr || w == nullptr)
            return nullptr;
            
        while (*s != 0)
        {
            while (*s != *w && *s != 0) ++s;
            
            char* sforward = s;
            char* wforward = w;
            while (*sforward == *wforward &&
                    *sforward != 0 && *wforward != 0)
                ++sforward, ++wforward;
    
            if (*wforward == 0)
                return s;
            else 
                ++s;
        }
        return nullptr;
    }
};
思路:

LeetCode有时候需要我们耍一下流氓。在检查的时候先看一下最后一个字符是不是match,如果不match就跳过。这其实没什么大意思。

题解:

class Solution {
public:
    char* strStr (char* s, char* w)
    {
        if (s == nullptr || w == nullptr)
            return nullptr;
            
        if (*w == 0)
            return s;
            
        const int LEN_S = std::strlen (s);
        const int LEN_W = std::strlen (w);
        
        char* s_iter = s;
        char* s_iter_end = s + (LEN_S - LEN_W);
        while (s_iter <= s_iter_end)
        {
            while (*s_iter != *w && s_iter < s_iter_end) ++s_iter;
            
            char* sforward = s_iter;
            char* wforward = w;
            
            if (* (s_iter + LEN_W - 1) == * (w + LEN_W - 1))
            {
                while (*sforward == *wforward && *wforward != 0)
                    ++sforward, ++wforward;
                    
                if (*wforward == 0)
                    return s_iter;
            }
            
            ++s_iter;
        }
        return nullptr;
    }
};


你可能感兴趣的:(LeetCode)