Skip to content

Commit 65634b4

Browse files
committed
ok
1 parent bbb72e6 commit 65634b4

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/com/leetcode/Main28.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.leetcode;
2+
3+
/**
4+
* 实现 strStr() 函数。
5+
*
6+
* 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1。
7+
*
8+
* 示例 1:
9+
*
10+
* 输入: haystack = "hello", needle = "ll"
11+
* 输出: 2
12+
*/
13+
public class Main28 {
14+
public int strStr(String haystack, String needle) {
15+
if(needle == null || needle.length() == 0) {
16+
return 0;
17+
}
18+
for (int i = 0; i < haystack.length() - needle.length() + 1; i++) {
19+
if (haystack.substring(i, i + needle.length()).equals(needle))
20+
return i;
21+
}
22+
return -1;
23+
}
24+
}

src/com/leetcode/Pack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.leetcode;
2-
2+
import java.util.HashMap;
33
public class Pack {
44
public static void main(String[] args) {
55
int[] w = {2, 1, 3, 2};

0 commit comments

Comments
 (0)