File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ > ``` java
2
+ > /**
3
+ > * 实现strStr()
4
+ > *
5
+ > */
6
+ > public class S1 {
7
+ > // public int strStr(String haystack, String needle) {
8
+ > // //直接调用indexof()
9
+ > // return haystack.indexOf(needle);
10
+ > // }
11
+ >
12
+ > public int strStr (String haystack , String needle ) {
13
+ > if (needle. length() == 0 )
14
+ > return 0 ;
15
+ >
16
+ > char [] charArrayHaystack = haystack. toCharArray();
17
+ > char [] charArrayNeedle = needle. toCharArray();
18
+ >
19
+ > for (int i = 0 ; i < charArrayHaystack. length; i++ ) {
20
+ > if (charArrayHaystack[i] == charArrayNeedle[0 ]){
21
+ > int j = i + 1 ;
22
+ > int k = 1 ;
23
+ > for (;k < charArrayNeedle. length && j < charArrayHaystack. length; k++ ) {
24
+ > if (charArrayHaystack[j] == charArrayNeedle[k])
25
+ > j++ ;
26
+ > else
27
+ > break ;
28
+ > }
29
+ > if (k == charArrayNeedle. length)
30
+ > return i;
31
+ > }
32
+ > }
33
+ >
34
+ > return - 1 ;
35
+ > }
36
+ >
37
+ > public static void main (String [] args ) {
38
+ > S1 s = new S1 ();
39
+ > int res = s. strStr(" hello" ," ll" );
40
+ > System . out. println(res);
41
+ > }
42
+ > }
43
+ > ```
44
+
You can’t perform that action at this time.
0 commit comments