Created
January 10, 2013 15:14
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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