Simple c++ code in 5ms


  • 2
    S
    class Solution {
    public:
        int strStr(char *haystack, char *needle) {
            int index = -1;
            char *head = haystack;
            int tokenLen = strlen(needle);
            while(strlen(head) >= tokenLen)
            {
                if(memcmp(head,needle,tokenLen) == 0)
                {
                    index = head - haystack;
                    break;
                }
                head++;
            }
            return index;
         }
    };

Log in to reply
 

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