## 28. Implement strStr()

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Analysis:

Source Code（C++）:

#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
int strStr(string haystack, string needle) {
int h_index=0, n_index=0;
while(h_index<haystack.size() && n_index<needle.size()) {
if (haystack.at(h_index) == needle.at(n_index)) {  //如果相等，继续对比下一位
h_index++;
n_index++;
}
else{
h_index = h_index-n_index+1;    //否则，h退回到上次匹配首位的下一位
n_index = 0;
}
}
if (n_index==needle.size()){
return h_index-needle.size();
}
else {
return -1;
}
}
};

int main() {
Solution sol;
cout << sol.strStr("", "");
cout << sol.strStr("", "a");
cout << sol.strStr("a", "");
cout << sol.strStr("avf", "vf");
return 0;
}

#### [28] Implement strStr()

2016-07-28 00:00:14

#### LeetCode 28 Implement strStr() (C,C++,Java,Python)

2015-05-12 11:59:17

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

2016-05-06 18:50:17

#### LeetCode --- 28. Implement strStr()

2015-02-02 11:54:14

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

2016-06-17 16:52:28

#### [leetcode] 28. Implement strStr() 解题报告

2015-12-28 16:32:40

#### [LeetCode] 028. Implement strStr() (Easy) (C++/Python)

2015-03-06 15:05:22

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

2016-06-19 01:25:02

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

2015-07-11 21:42:14

#### [LeetCode-28] Implement strStr（KMP 字符串匹配）

2015-11-20 17:59:41

## 不良信息举报

28. Implement strStr()