Run my computer is right but wrong in leetcode with same input


  • 0
    H

    int strStr(char* haystack, char* needle) { //needle is substring of haystack or not

    int lh=strlen(haystack);
    int ln=strlen(needle);
    if(ln>lh) return -1;
    char *bijiao="";
    if(!strcmp(bijiao,needle)) return 0;
    if(!strcmp(haystack,needle)) return 0;
    char tmp[ln+1],first=needle[0];
    int i;
    for(i=0;i<lh;i++)
    {
    if(haystack[i]==first)
    { strncpy(tmp,&haystack[i],ln);
    if(strcmp(needle,tmp)==0) return i;

    }
    

    }

    return -1;

    }


Log in to reply
 

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