Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
就是找到子串第一次出现的 位置。
public int strStr(String haystack, String needle) {
if (haystack == null || needle == null) {
return -1;
}
for (int i = 0; i < haystack.length() - needle.length() +

该博客介绍了LeetCode上的第28题,内容是实现strStr()函数,找到字符串haystack中子串needle的首次出现位置。文章通过分析指出了判断条件应为j == needle.length(),而非j == needle.length()-1,解释了为何for循环结束条件正确。
订阅专栏 解锁全文
296

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



