问题 : https://oj.leetcode.com/problems/implement-strstr/
我的解答:
int strStr(char *haystack, char *needle) {
if (!*needle) return 0;
if (!*haystack) return -1;
char* ph, *pn;
ph = haystack;
for (int i = 0;*ph; ++i, ++ph)
{
char* py = ph;
pn = needle;
while (*py && *pn && *py == *pn)
{
++py;
++pn;
}
if (!*pn)
return i;
}
return -1;
}
虽然知道代码的效率有待改进,比如haystack末尾长度少于needle的不用检测,用KMP等,但是就上面的代码,我觉得虽然效率低,但是应该可以通过,结果提示Time Limit Exceeded, 我就想是不是我的代码真的有问题,但是在本地运行好像得到的结果还对呢,这是我的问题,还是online judge的问题呢