FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/strstr.java at master · wpande/leetcode · GitHub
wpande
leetcode
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
strstr.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
42 lines (42 loc) · 1.06 KB
Breadcrumbs
leetcode
/
strstr.java
Copy path
File metadata and controls
42 lines (42 loc) · 1.06 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
32
33
34
35
36
37
38
39
40
41
42
public
class
Solution
{
int
[]
fneedle
(
String
needle
){
int
len
=
needle
.
length
();
int
[]
fn
=
new
int
[
len
];
for
(
int
i
=
0
;
i
<
len
;
i
++)
fn
[
i
]=
i
-
1
;
//begin at -1
for
(
int
i
=
1
;
i
<
len
;
i
++){
int
j
=
i
-
1
;
while
(
fn
[
j
]>=
0
&&
needle
.
charAt
(
fn
[
j
])!=
needle
.
charAt
(
i
-
1
))
j
=
fn
[
j
];
fn
[
i
]=
fn
[
j
]+
1
;
}
return
fn
;
}
public
int
strStr
(
String
haystack
,
String
needle
) {
if
(
needle
.
length
()<
1
)
return
0
;
if
(
haystack
.
length
()<
1
)
return
-
1
;
int
i
=
0
;
int
j
=
0
;
int
res
=-
1
;
int
[]
fn
=
fneedle
(
needle
);
while
(
i
<
haystack
.
length
()){
if
(
haystack
.
charAt
(
i
)==
needle
.
charAt
(
j
)){
i
++;
j
++;
if
(
j
==
needle
.
length
()){
res
=
i
-
j
;
break
;
}
}
else
{
j
=
fn
[
j
];
if
(
j
<
0
){
j
=
0
;
i
++;
}
}
}
return
res
;
}
}
Back
|
FazBrowse Home
|
New Git URL