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

Commit95dbb95

Browse files
EASY/src/easy/ImplementStrStr.java
1 parent34e6d77 commit95dbb95

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

‎EASY/src/easy/ImplementStrStr.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
packageeasy;
2+
3+
publicclassImplementStrStr {
4+
/**You could use substring as follows, or use two pointers to go through the haystack, if substring API call is not allowed.*/
5+
publicstaticintstrStr(Stringhaystack,Stringneedle) {
6+
if(haystack ==null ||needle ==null){
7+
return -1;
8+
}
9+
if(haystack.isEmpty()){
10+
returnneedle.isEmpty() ?0 : -1;
11+
}
12+
if(needle.isEmpty()){
13+
return0;
14+
}
15+
for(inti =0;i <haystack.length() -needle.length()+1;i++){
16+
if(haystack.substring(i,i+needle.length()).equals(needle))returni;
17+
}
18+
return -1;
19+
}
20+
21+
publicstaticvoidmain(String...args){
22+
// String haystack = "a";
23+
// String needle = "";
24+
25+
// String haystack = "mississippi";
26+
// String needle = "a";
27+
28+
Stringhaystack ="a";
29+
Stringneedle ="a";
30+
strStr(haystack,needle);
31+
}
32+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp