题目
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) {
if(needle==NULL||haystack==NULL)
return NULL;
int lenhay=strlen(haystack);
int lenneed=strlen(needle);
if(lenhay<lenneed)
return NULL;
char *p,*q;
for(int i=0;i<=lenhay-lenneed;i++)
{
p=haystack+i;
q=needle;
while(*p!='\0')
{
if(*p!=*q)
break;
p++;
q++;
}
if(*q=='\0')
return haystack+i;
}
return NULL;
}
};
本文介绍了一个用于在主字符串中查找子字符串的算法实现,并详细解释了其工作原理。
221

被折叠的 条评论
为什么被折叠?



