本文主要是介绍LeetCode 28:Implement strStr(),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
实现函数strStr。代码如下:
int strStr(char* haystack, char* needle) {size_t len = strlen(haystack);if(strlen(needle) == 0)return 0;if(strcmp(haystack, needle) == 0)return 0;for(int i=0; i<len;i++){if(haystack[i] == needle[0]){if(strncmp(&(haystack[i]), needle, strlen(needle)) == 0)return i;}}return -1;}
这篇关于LeetCode 28:Implement strStr()的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!