您的位置:首页 > 其它

[LeetCode] Implement strStr()

2012-11-26 20:51 435 查看
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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int hayLen = strlen(haystack);
int needLen = strlen(needle);

for(int i = 0; i <= hayLen - needLen; i++)
{
char *p = haystack + i;
char *q = needle;
while(*q != '\0')
{
if (*p != *q)
break;
else
{
p++;
q++;
}
}

if (*q == '\0')
return haystack + i;
}

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