# Leetcode 28, Strstr

//思路：双指针，因为两个string肯定二个循环。遍历整个长字符（减去短字符）作为起点，双指针长字符和短字符逐一比较。

//答案综合https://leetcode.com/discuss/95153/java-easy-to-understand-solutions（双指针比较清晰）和爱做饭第一个（答案返回string过时了，

//第二个没必要）。

public int strStr(String haystack, String needle) {
if (haystack == null || needle == null)
return -1;
for (int i = 0; i <= haystack.length() - needle.length(); i++) {
int hayPointer = i;
int needlePointer = 0;
while (needlePointer < needle.length() && haystack.charAt(hayPointer) == needle.charAt(needlePointer)) {
hayPointer++;
needlePointer++;
}
if (needlePointer == needle.length()){
return i;
}
}
return -1;
}

#### 28. Implement strStr()-leetcode-java

2016-05-06 18:50:17

#### 【leetcode】Python实现-28.实现strStr()

2018-05-11 17:08:16

#### leetcode 28 Implement strStr() C++

2016-06-17 16:52:28

#### [leetcode-28]implement strstr()(C语言)

2015-07-11 21:42:14

#### LeetCode 28. Implement strStr()（实现子串定位）

2016-05-20 10:35:48

#### [leetcode:python]28.Implement strStr()

2017-05-10 19:40:00

#### leetcode 28. 实现strStr()

2018-04-19 20:13:26

#### 28. Implement strStr() [easy] (Python)

2016-06-19 01:25:02

#### leetcode | Implement strStr() | 实现字符串查找函数

2015-07-05 15:29:44