LeetCode-Implement strStr()
2014-08-17 18:44
344 查看
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
Solution:
Code:
<span style="font-size:14px;">class Solution {
public:
char *strStr(char *haystack, char *needle) {
const int lengthH = strlen(haystack);
const int lengthN = strlen(needle);
for (int i = 0; i <= lengthH-lengthN; ++i) {
bool match = true;
for (int j = 0; j < lengthN; ++j)
if (haystack[i+j] != needle[j]) {
match = false;
break;
}
if (match) return &haystack[i];
}
return NULL;
}
};</span>
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
Solution:
Code:
<span style="font-size:14px;">class Solution {
public:
char *strStr(char *haystack, char *needle) {
const int lengthH = strlen(haystack);
const int lengthN = strlen(needle);
for (int i = 0; i <= lengthH-lengthN; ++i) {
bool match = true;
for (int j = 0; j < lengthN; ++j)
if (haystack[i+j] != needle[j]) {
match = false;
break;
}
if (match) return &haystack[i];
}
return NULL;
}
};</span>
相关文章推荐
- [Leetcode] Implement strStr()
- leetcode之Implement strStr()
- [LeetCode28] Implement strStr()
- leetcode---Implement strStr()
- leetcode 28: Implement strStr() (KMP算法)
- leetcode——Implement strStr() 实现字符串匹配函数(AC)
- [LeetCode] [Implement strStr() 2012-02-18]
- 【Leetcode】【Easy】Implement strStr()
- LeetCode: Implement strStr()
- leetcode Implement strStr()
- Leetcode Implement strStr()
- LeetCode:Implement strStr()
- LeetCode:Implement strStr()
- [leetcode]Implement strStr()
- leetcode(57)- Implement strStr()
- [leetcode] Implement strStr()
- 【LEETCODE】Implement strStr().
- 【leetcode】3.2 Implement strStr()
- LeetCode -- Implement strStr()
- [LeetCode] Implement strStr()