Skip to content

Commit 0be8e9b

Browse files
author
applewjg
committed
update strStr()
Change-Id: I0afed8005854afe5a4904a7be032bd6acc8e7a4c
1 parent de27c6a commit 0be8e9b

File tree

1 file changed

+77
-18
lines changed

1 file changed

+77
-18
lines changed

ImplementstrStr().h

Lines changed: 77 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,90 @@
11
/*
2-
Author: Annie Kim, [email protected]
3-
Date: Apr 9, 2013
4-
Update: Jul 19, 2013 (Refactor)
2+
Author: King, [email protected]
3+
Date: Dec 17, 2014
54
Problem: Implement strStr()
65
Difficulty: Easy
7-
Source: http://leetcode.com/onlinejudge#question_28
6+
Source: https://oj.leetcode.com/problems/implement-strstr/
87
Notes:
98
Implement strStr().
109
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
1110
12-
Solution: Check in the haystack one by one. If not equal to needle, reset the pointer.
11+
Solution: 1. Check in the haystack one by one. If not equal to needle, reset the pointer.(TLE)
12+
2. Classice KMP solution.
13+
3. Simplified RK Soluiton. Thanks for [wenyuanhust, [email protected]]
1314
*/
14-
1515
class Solution {
1616
public:
17-
char *strStr(char *haystack, char *needle) {
18-
while(true)
19-
{
20-
char *h = haystack, *n = needle;
21-
while (*n != '\0' && *h == *n)
22-
{
23-
h++;
24-
n++;
17+
int strStr_1(char *haystack, char *needle) {
18+
int sLen = strlen(haystack);
19+
const int tLen = strlen(needle);
20+
if (tLen == 0) return 0;
21+
if(haystack==NULL || needle==NULL || sLen==0) return -1;
22+
int i = 0, j =0;
23+
while (i < sLen) {
24+
j = 0;
25+
int k = i;
26+
while (j < tLen && haystack[k] == needle[j]) {
27+
++k, ++j;
28+
}
29+
if (j == tLen) return k - j;
30+
++i;
31+
}
32+
return -1;
33+
}
34+
void getNext(char * T, int next[], int n){
35+
int i=0, j=-1;
36+
next[0]=-1;
37+
while(i<n){
38+
while(j>-1&&T[j]!=T[i]) j = next[j];
39+
i++,j++;
40+
if(T[j]==T[i]) next[i]=next[j];
41+
else next[i]=j;
42+
}
43+
}
44+
int strStr_2(char *haystack, char *needle) {
45+
int sLen = strlen(haystack);
46+
const int tLen = strlen(needle);
47+
if (tLen == 0) return 0;
48+
if(haystack==NULL || needle==NULL || sLen==0) return -1;
49+
50+
int next[tLen+1];
51+
getNext(needle,next,tLen);
52+
53+
int i=0, j=0;
54+
while(i<sLen){
55+
while(j>-1&&needle[j]!=haystack[i]) j = next[j];
56+
i++,j++;
57+
if(j==tLen){
58+
return i - j;
59+
}
60+
}
61+
return -1;
62+
}
63+
int strStr_3(char *haystack, char *needle) {
64+
if (needle && *needle == '\0') return 0;
65+
if (haystack == NULL || needle == NULL || *haystack == '\0') return -1;
66+
long long fh = 0, fn = 0;
67+
char *n = needle, *tail = haystack, *head = haystack;
68+
while (*n) {
69+
if (*tail == '0') return -1;
70+
fh += *tail;
71+
fn += *n;
72+
++n, ++tail;
73+
}
74+
--tail;
75+
while (*tail) {
76+
if (fn == fh) {
77+
char *pTemp = head,*nTemp = needle;
78+
while (*nTemp && *nTemp == *pTemp) {
79+
++nTemp; ++pTemp;
80+
}
81+
if (*nTemp == '\0') return head - haystack;
2582
}
26-
if (*n == '\0') return haystack;
27-
if (*h == '\0') return NULL;
28-
haystack++;
83+
fh -= *head;
84+
++tail;
85+
++head;
86+
fh += *tail;
2987
}
88+
return -1;
3089
}
31-
};
90+
};

0 commit comments

Comments
 (0)