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

Add solution for problem 28#179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ashmichheda wants to merge2 commits intofishercoder1534:master
base:master
Choose a base branch
Loading
fromashmichheda:28-fix
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletionssrc/main/java/com/fishercoder/solutions/_28.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -17,4 +17,21 @@ public int strStr(String haystack, String needle) {
}
}

public static class Solution2 {
public int strStr(String haystack, String needle) {

int n = needle.length();
int h = haystack.length();

for (int i = 0; i <= h - n; i++) {
for (int j = 0; j < n && haystack.charAt(i + j) == needle.charAt(j); j++) {
if (j == n - 1) {
return i;
}
}
}
return -1;
}
}

}
5 changes: 5 additions & 0 deletionssrc/test/java/com/fishercoder/_28Test.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,20 +8,25 @@

public class _28Test {
private static _28.Solution1 solution1;
private static _28.Solution2 solution2;

@Before
public void setupForEachTest() {
solution1 = new _28.Solution1();
solution2 = new _28.Solution2();
}

@Test
public void test1() {
assertEquals(0, solution1.strStr("a", ""));
assertEquals(0, solution2.strStr("sadbutsad", "sad"));
}

@Test
public void test2() {

assertEquals(-1, solution1.strStr("mississippi", "a"));
assertEquals(8, solution2.strStr("leetcodea", "a"));
}

@Test
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp