LeetCode28. Implement strStr()
题目:
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = "hello", needle = "ll" Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba" Output: -1
题目分析:直接用string的find函数
代码:
class Solution {
public:
int strStr(string haystack, string needle) {
int tmp = haystack.find(needle);
if (tmp == string::npos)
return -1;
return tmp;
}
};
本文解析了LeetCode上的第28题“Implement strStr()”,并提供了一个简洁的C++实现方案,该方案利用了string类的find成员函数来查找子串首次出现的位置。
509

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



