Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaea06ab

Browse files
author
applewjg
committed
Implement strStr()
Change-Id: I86bbe790709ef68063af7fd696e893d4b064feb7
1 parentf6b2eea commitaea06ab

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

‎ImplementstrStr().java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 17, 2014
4+
Problem: Implement strStr()
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/implement-strstr/
7+
Notes:
8+
Implement strStr().
9+
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
10+
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, wenyuanhust@gmail.com]
14+
*/
15+
16+
publicclassSolution {
17+
publicintstrStr_1(Stringhaystack,Stringneedle) {
18+
intsLen =haystack.length(),tLen =needle.length();
19+
if(tLen ==0)return0;
20+
if (haystack==null ||needle==null ||sLen==0)return -1;
21+
inti =0,j =0;
22+
while (i <sLen) {
23+
for (j =0;j <tLen && (i+j) <sLen &&haystack.charAt(i+j) ==needle.charAt(j); ++j);
24+
if (j ==tLen)returni;
25+
++i;
26+
}
27+
return (int)-1;
28+
}
29+
voidgetNext(StringT,int[]next){
30+
inti=0,j=-1;
31+
next[0]=-1;
32+
intn =next.length;
33+
while(i <n -1){
34+
while(j>-1&&T.charAt(j)!=T.charAt(i))j =next[j];
35+
++i; ++j;
36+
if(i <n -1 &&j <n -1 &&T.charAt(j)==T.charAt(i))next[i]=next[j];
37+
elsenext[i]=j;
38+
}
39+
}
40+
publicintstrStr_2(Stringhaystack,Stringneedle) {
41+
intsLen =haystack.length(),tLen =needle.length();
42+
if(tLen ==0)return0;
43+
if (haystack==null ||needle==null ||sLen==0)return -1;
44+
int[]next =newint[tLen+1];
45+
getNext(needle,next);
46+
inti =0,j =0;
47+
while (i <sLen) {
48+
while (j > -1 &&needle.charAt(j) !=haystack.charAt(i))j =next[j];
49+
++i; ++j;
50+
if (j ==tLen)returni -j;
51+
}
52+
return -1;
53+
}
54+
publicintstrStr_3(Stringhaystack,Stringneedle) {
55+
intsLen =haystack.length(),tLen =needle.length();
56+
if (tLen ==0)return0;
57+
if (haystack==null ||needle==null ||sLen==0 ||sLen <tLen)return -1;
58+
longfh =0,fn =0;
59+
inthead =0,tail =tLen -1;
60+
for (inti =0;i <tLen; ++i) {
61+
fh +=haystack.charAt(i);
62+
fn +=needle.charAt(i);
63+
}
64+
while (tail <sLen) {
65+
if (fn ==fh) {
66+
inti =0;
67+
while (i <tLen &&needle.charAt(i) ==haystack.charAt(i +head)){
68+
++i;
69+
}
70+
if (i ==tLen)returnhead;
71+
}
72+
if (tail ==sLen -1)return -1;
73+
fh -=haystack.charAt(head++);
74+
fh +=haystack.charAt(++tail);
75+
}
76+
return -1;
77+
}
78+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp