Implement strStr()

Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.



对KMP还不太熟,直接匹配了

class Solution {
public:
	char *strStr(char *haystack, char *needle) {
	     if(needle==NULL||*needle=='\0')
            return haystack;
		string hs(haystack);
		string nd(needle);
		int j = 0;
		for (int i = 0; i < hs.length(); ++i)
		{
			if (hs[i] != nd[j])
			{
				if (j != 0)
				{
					i=i-j;
					j = 0;
				}
				continue;
			}
			if (j == nd.length()-1)
				return &haystack[i-j];
			++j;
		}
		return NULL;
	}
};




你可能感兴趣的:(LeetCode)