[LeetCode] Implement strStr()

Implement strStr()

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

解题思路:

1、暴力法。逐一匹配,然后回溯。代码如下,但是产生超时错误。

class Solution {
public:
    int strStr(string haystack, string needle) {
        int len1=haystack.length();
        int len2=needle.length();
        if(len2>len1){
            return -1;
        }
        for(int i=0; i<len1; i++){
            int j=0;
            for(; j<len2&&i+j<len1; j++){
                if(haystack[i+j]!=needle[j]){
                    break;
                }
            }
            if(j==len2){
                return i;
            }
        }
        return -1;
    }
};

2、KMP算法。

class Solution {
public:
    int strStr(string haystack, string needle) {
        int len1=haystack.length();
        int len2=needle.length();
        if(len2>len1){
            return -1;
        }
        if(len2==0){
            return 0;
        }
        int next[len2];
        next[0]=-1;
        int i=0, j=-1;
        
        while(i<len2){
            if(j==-1||needle[j]==needle[i]){
                i++;
                j++;
                next[i]=j;
            }else{
                j=next[j];
            }
        }
        
        i=0; j=0;
        while(i<len1){
            if(j==-1||haystack[i]==needle[j]){
                i++;
                j++;
            }else{
                j=next[j];
            }
            if(j==len2){
                return i-len2;
            }
        }
        
        return -1;
    }
};

二次刷题(update in 2015-07-31)

1、同样的暴力法,这次竟然很快的过了,看来编码规范还是非常重要的:

class Solution {
public:
    int strStr(string haystack, string needle) {
        int m = haystack.length();
        int n = needle.length();
        if(m<n){
            return -1;
        }
        for(int i=0; i < m - n + 1; i++){
            int j = 0;
            while(j<n){
                if(haystack[i+j]==needle[j]){
                    j++;
                }else{
                    break;
                }
            }
            if(j==n){
                return i;
            }
        }
        return -1;
    }
};

KMP算法老是不懂,next数组的求法需要着重记一下。参考博客:http://blog.csdn.net/v_july_v/article/details/7041827

class Solution {
public:
    int strStr(string haystack, string needle) {
        int m = haystack.length();
        int n = needle.length();
        if(m<n){
            return -1;
        }
        if(n==0){
            return 0;
        }
        int next[n];
        next[0] = -1;
        int i = 0, j = -1;
        while(i<n-1){
            // needle[j] 与 needle[i] 分别是前缀和后缀字符串的最后一个字符
            if(j==-1 || needle[j] == needle[i]){
                j++;
                i++;
                next[i]=j;
            }else{
                j = next[j];
            }
        }
        i = 0, j = 0;
        while(i<m && j<n){
            if(j == -1 || haystack[i] == needle[j]){
                i++;
                j++;
            }else{
                j = next[j];
            }
        }
        if(j==n){
            return i - j;
        }else{
            return -1;
        }
    }
};


0 条评论

    发表评论

    电子邮件地址不会被公开。 必填项已用 * 标注