-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-28.cpp
More file actions
31 lines (28 loc) · 955 Bytes
/
LeetCode-28.cpp
File metadata and controls
31 lines (28 loc) · 955 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*************************************************************************
> File Name: LeetCode-28.cpp
> Author: ltw
> Mail: 3245849061@qq.com
> Created Time: Tue 19 May 2020 08:42:38 PM CST
************************************************************************/
#include <iostream>
using namespace std;
class Solution {
public:
int strStr(string haystack, string needle) {
int ind[256];
for (int i = 0; i < 256; i++) ind[i] = needle.size() + 1;
for (int i = 0; needle[i]; i++) {
ind[needle[i]] = needle.size() - i;
}
for (int i = 0, I = haystack.size() - needle.size(); i <= I; ) {
int j = 0;
for (; needle[j]; j++) {
if (haystack[i + j] == needle[j]) continue;
i += ind[haystack[i + needle.size()]];
break;
}
if (needle[j] == 0) return i;
}
return -1;
}
};