解题思路: 这个网络上已经有很多答案,这里
列举一个复杂度为0(m*n)的例子。实现的原理
很简单,顺序遍历要查找的字符串,如果没有找
到,字符串指针往前一位,再往后比较要查找的
字符串(模式串),如下图所示:
a b c d d d a d a c a b d a d d d a d c k
->
d a d a
char* strstr(const char* s1, const char* s2)
{
if(s1 == NULL || s2 == NULL)
return NULL;
while(*s1)
{
int i = 0;
while(1)
{
if(s2[i] == NULL)
return (char*)s1;
if(s2[i] != s1[i])
break;
i++;
}
s1++;
}
return NULL;
}