您的位置:首页 > 其它

LeetCode Implement strStr()

2014-05-25 13:09 288 查看
题目

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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: