您的位置:首页 > 其它

LeetCode-Implement strStr()

2014-08-17 18:44 344 查看
Implement strStr().

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

<span style="font-size:14px;">class Solution {
public:
char *strStr(char *haystack, char *needle) {
const int lengthH = strlen(haystack);
const int lengthN = strlen(needle);
for (int i = 0; i <= lengthH-lengthN; ++i) {
bool match = true;
for (int j = 0; j < lengthN; ++j)
if (haystack[i+j] != needle[j]) {
match = false;
break;
}
if (match) return &haystack[i];
}
return NULL;
}
};</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息