Skip to content

Commit 65491bb

Browse files
authored
Merge pull request #1 from PYTWSW/PYTWSW-patch-1
Create WhiteNight.md
2 parents 036c275 + 4ac55db commit 65491bb

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

2019.11.24/WhiteNight.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+

0 commit comments

Comments
 (0)