LeetCode 28 Implement strStr() (C,C++,Java,Python)

Problem:

Implement strStr().

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

Update (2014-11-02):
The signature of the function had been updated to return the index instead of the pointer. If you still see your function signature returns a char * or String, please click the reload button  to reset your code definition.

Solution:

O(nm) runtime, O(1) space – Brute force:

You could demonstrate to your interviewer that this problem can be solved using known efficient algorithms such as Rabin-Karp algorithm, KMP algorithm, and the Boyer- Moore algorithm. Since these algorithms are usually studied in an advanced algorithms class, it is sufficient to solve it using the most direct method in an interview – The brute force method.

The brute force method is straightforward to implement. We scan the needle with the haystack from its first position and start matching all subsequent letters one by one. If one of the letters does not match, we start over again with the next position in the haystack.

The key is to implement the solution cleanly without dealing with each edge case separately.

经典的KMP应用,暴力什么的还是不要提了,参考这里: 从头到尾彻底理解KMP


题目大意:

给两个字符串,求第二个字符串在第一个字符串中出现的最小位置,如果没有出现则输出-1


Java源代码(323ms):

public class Solution {
    public int strStr(String haystack, String needle) {
        char[] chs1=haystack.toCharArray();
        char[] chs2=needle.toCharArray();
        int len1=chs1.length,len2=chs2.length;
        int[] next=new int[len2+1];
        getNext(chs2,next,len2);
        int i=0,j=0;
        while(i<len1 && j<len2){
            if(j==-1 || chs1[i]==chs2[j]){
                i++;j++;
            }else{
                j=next[j];
            }
        }
        if(j<len2)return -1;
        return i-len2;
    }
    private void getNext(char[] chs,int[] next,int len){
        int i=0,j=-1;
        next[0]=-1;
        while(i<len){
            if(j==-1 || chs[i]==chs[j]){
                i++;j++;
                next[i]=j;
            }else{
                j=next[j];
            }
        }
    }
}


C语言源代码(2ms):

void getNext(char *needle,int* next){
    int i=0,j=-1;
    next[0]=-1;
    while(needle[i]){
        if(j==-1 || needle[j]==needle[i]){
            j++;
            i++;
            next[i]=j;
        }else{
            j=next[j];
        }
    }
}
int strStr(char* haystack, char* needle) {
    int length=strlen(needle),i=0,j=0;
    int *next=(int*)malloc(sizeof(int)*(length+1));
    getNext(needle,next);
    while(j==-1 || (haystack[i] && needle[j])){
        if(j==-1 || haystack[i]==needle[j]){
            j++;i++;
        }else{
            j=next[j];
        }
    }
    if(needle[j])return -1;
    else return i-length;
}

C++源代码(8ms):

class Solution {
public:
    int strStr(string haystack, string needle) {
        int len1=haystack.size(),len2=needle.size();
        int* next=(int*)malloc(sizeof(int)*(len2+1));
        getNext(needle,next,len2);
        int i=0,j=0;
        while(i<len1 && j<len2){
            if(j==-1 || haystack[i]==needle[j]){
                i++;j++;
            }else{
                j=next[j];
            }
        }
        if(j<len2)return -1;
        else return i-len2;
    }
private:
    void getNext(string needle,int* next,int len1){
        int i=0,j=-1;
        next[0]=-1;
        while(i<len1){
            if(j==-1 || needle[i]==needle[j]){
                j++;i++;
                next[i]=j;
            }else{
                j=next[j];
            }
        }
    }
};

Python源代码(74ms):

class Solution:
    # @param {string} haystack
    # @param {string} needle
    # @return {integer}
    def strStr(self, haystack, needle):
        len1=len(haystack);len2=len(needle)
        next=[-1 for i in range(len2+1)]
        self.getNext(needle,next,len2)
        i=0;j=0
        while i<len1 and j<len2:
            if j==-1 or haystack[i]==needle[j]:
                i+=1;j+=1
            else:j=next[j]
        if j<len2:return -1
        else:return i-len2
    def getNext(self,needle,next,len2):
        i=0;j=-1
        while i<len2:
            if j==-1 or needle[i]==needle[j]:
                i+=1
                j+=1
                next[i]=j
            else:j=next[j]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值