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

Commit3ca9bdc

Browse files
authored
Merge pull requestneetcode-gh#2331 from benmak11/0028-find-index-of-first-occurence-in-string
Create 0028-find-the-index-of-the-first-occurence-in-a-string
2 parents48eed3a +3836ac0 commit3ca9bdc

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

‎java/0028-find-the-index-of-the-first-occurrence-in-a-string.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Solution One
12
publicclassSolution {
23
publicintstrStr(Stringhaystack,Stringneedle) {
34
if (needle.isEmpty()) {
@@ -40,3 +41,17 @@ public int strStr(String haystack, String needle) {
4041
}
4142
}
4243

44+
// Solution Two (Linear search using indexOf method for arrays)
45+
classSolution {
46+
publicintstrStr(Stringhaystack,Stringneedle) {
47+
intresult =0;
48+
if (haystack.length() <=0 &&needle.length() >0)return -1;
49+
if (haystack.length() !=0) {
50+
intoccurence =haystack.indexOf(needle);
51+
if (occurence == -1)
52+
returnoccurence;
53+
result =occurence;
54+
}
55+
returnresult;
56+
}
57+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp