My c solution


  • 0
    T
    int strStr(char* haystack, char* needle) {
        int length_n=strlen(needle);
        if(length_n==0)
            return 0;
        int length_h=strlen(haystack);
        if(length_h<length_n)
            return -1;
        int i;
        for(i=0;i<=length_h-length_n;i++)
        {
            if(haystack[i]==needle[0])    
                if(!strncmp(&haystack[i],needle,length_n))
                    return i;
        }
        return -1;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.