- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo28.java
31 lines (27 loc) · 630 Bytes
/
No28.java
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
packageAlgorithm.leetcode.String;
publicclassNo28 {
publicintstrStr(Stringhaystack, Stringneedle) {
if (needle == "" || needle == null) {
return0;
}
if (haystack.contains(needle)) {
returnhaystack.indexOf(needle);
}
return -1;
}
publicintstrStr02(Stringhaystack, Stringneedle) {
inthayLen = haystack.length(), needLen = needle.length();
if (hayLen < needLen) {
return -1;
} elseif (needLen == 0) {
return0;
}
intdum = hayLen - needLen;
for (inti = 0; i <= dum; i++) {
if ((haystack.substring(i, i + needLen)).equals(needle)) {
returni;
}
}
return -1;
}
}