首页 > 代码库 > leetcode - Implement strStr()

leetcode - Implement strStr()

Implement strStr().

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

class Solution {
public:
    char *strStr(char *haystack, char *needle) {
        int i = 0, j = 0;
		while (haystack[i] && needle[j])
		{
			if(haystack[i] == needle[j])
			{
				++i;
				++j;
			}
			else
			{
				i = i - j + 1;
				j = 0;
			}
		}
		return needle[j] ? 0 : haystack + i - j;
    }
};


leetcode - Implement strStr()