LeetCode Implement strStr()
2014-05-25 13:09
288 查看
题目
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
即寻找子串
代码:
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
即寻找子串
代码:
class Solution { public: char *strStr(char *haystack, char *needle) { if(needle==NULL||haystack==NULL) return NULL; int lenhay=strlen(haystack); int lenneed=strlen(needle); if(lenhay<lenneed) return NULL; char *p,*q; for(int i=0;i<=lenhay-lenneed;i++) { p=haystack+i; q=needle; while(*p!='\0') { if(*p!=*q) break; p++; q++; } if(*q=='\0') return haystack+i; } return NULL; } };
相关文章推荐
- leetcode 第27-28 题 Implement strStr() & Divide Two Integers
- [LeetCode] Implement strStr()
- c语言 Implement strStr()【Leetcode】
- Leetcode: Implement strStr()
- [LeetCode]Implement strStr()
- LeetCode - Implement strStr()
- [leetcode]Implement strStr()
- Leetcode---Implement strStr()
- LeetCode-28 Implement strStr() (找出字串位置)
- Leetcode[easy] - Implement strStr()
- leetcode - Implement strStr()
- LeetCode Implement strStr()
- [leetcode]Implement strStr()
- LeetCode 28 Implement strStr() 找到字串返回位置。
- LeetCode-- Implement strStr()
- LeetCode---Implement strStr()
- Leetcode28: Implement strStr()
- [LeetCode-28] Implement strStr(KMP 字符串匹配)
- 【Leetcode】Implement strStr()
- LeetCode-Implement strStr()