File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed
Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11package com .leetcode ;
2-
2+ import java . util . HashMap ;
33public class Pack {
44 public static void main (String [] args ) {
55 int [] w = {2 , 1 , 3 , 2 };
You can’t perform that action at this time.
0 commit comments