Sharing 4ms c++ solution


  • 0
    T
       int strStr(string haystack, string needle) {
    	int n1 = haystack.length();
        int n2 = needle.length();
    	if(n2==0)
            return 0;
        if(n2>n1)
            return -1;
    	for(int i = 0;i < n1 - n2+1;i++)
    	{
    		for(int j = 0;j < n2;j++)
    		{
    			if(needle[j] != haystack[i+j])
    				break;
    			if(j+1 == needle.length())
    				return i;
    		}
    	}
    	return -1;
    }

Log in to reply
 

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