Skip to content

Instantly share code, notes, and snippets.

@pdu
Created January 10, 2013 15:14
Show Gist options
  • Select an option

  • Save pdu/4502784 to your computer and use it in GitHub Desktop.

Select an option

Save pdu/4502784 to your computer and use it in GitHub Desktop.
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. http://leetcode.com/onlinejudge
class Solution {
public:
char *strStr(char *haystack, char *needle) {
if (*haystack == NULL && *needle == NULL)
return haystack;
char* ret;
for (ret = haystack; *ret != NULL; ++ret) {
int k;
for (k = 0; needle[k] != NULL; ++k)
if (ret[k] != needle[k])
break;
if (needle[k] == NULL)
return ret;
if (ret[k] == NULL)
break;
}
return NULL;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment