leetcode 28. Implement strStr()
2017-04-05 17:08
330 查看
[Leetcode 28] Implement strStr() 子串匹配,返回第一次出现的位置,C语言实现
DescriptionImplement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
int strStr(char* haystack, char* needle) { int mlength, slength; int i, j; int k; mlength = strlen(haystack); slength = strlen(needle); if(mlength == 0 && slength == 0) return 0;//可省略 if(mlength < slength) return -1; for(i = 0; i <= mlength - slength; i++) { k = i; j = 0; while(k <= mlength && j < slength && haystack[k] == needle[j]) { k++; j++; } if(j == slength) { if(haystack[k - 1] == needle[j - 1]) return i;//判断最后一个字符是否也匹配 } } return -1; }
相关文章推荐
- [leetcode]28. Implement strStr()
- LeetCode 28. Implement strStr()
- LeetCode 28. Implement strStr()(字符串匹配问题)
- LeetCode --- 28. Implement strStr()
- leetcode-28. Implement strStr()
- [Leetcode] 28. Implement strStr() 解题报告
- [LeetCode] 28. Implement strStr() ☆
- 【leetcode】28. Implement strStr()(easy) KMP算法
- [leetcode] 28. Implement strStr()
- leetcode 28. Implement strStr()
- leetcode 28. Implement strStr()
- LeetCode-28. Implement strStr()
- [LeetCode]28. Implement strStr()
- LeetCode 28. Implement strStr()
- [勇者闯LeetCode] 28. Implement strStr()
- 【Leetcode】:28. Implement strStr()问题 in JAVA
- LeetCode 28. Implement strStr()
- 【LeetCode】28. Implement strStr() (2 solutions)
- LeetCode 28. Implement strStr()
- [LeetCode]28. Implement strStr()