class Solution {
public:
int strStr(string haystack, string needle) {
if (true == needle.empty()) {
return 0;
}
if (true == haystack.empty()) {
return -1;
}
int size0 = haystack.size();
int size1 = needle.size();
int i = 0;
int j = 0;
int k = 0;
while (i < size0) {
k = i;
while (j < size1 && haystack[k] == needle[j]) {
k++;
j++;
continue;
}
if (j == size1) {
return i;
}
i++;
j = 0;
}
return -1;
}
};


本文介绍了一个使用C++实现的字符串查找算法,该算法通过遍历主字符串并比较子字符串来确定子字符串是否存在主字符串中及其起始位置。具体实现包括了对空字符串和不匹配情况的处理。
2537

被折叠的 条评论
为什么被折叠?



