Skip to content

Commit 5670964

Browse files
authored
Merge pull request #1 from lliuquan/lliuquan-patch-1
Create Whits.md
2 parents 72f574a + 8e453b2 commit 5670964

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

2019.11.24/Whits.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package AC;
2+
3+
import java.lang.reflect.Array;
4+
5+
public class leetcode_28_AC {
6+
7+
8+
9+
public static void main(String[] args) {
10+
11+
String haystack = "hello";
12+
String needle = "ll";
13+
System.out.println(strStrNew(haystack,needle));
14+
15+
}
16+
public static int strStr(String haystack, String needle) {
17+
if (needle.length()==0)
18+
return 0;
19+
char[]hayStackCharArray =haystack.toCharArray();
20+
char[]needleCharArray = needle.toCharArray();
21+
for (int i = 0; i < hayStackCharArray.length; i++) {
22+
if (hayStackCharArray[i] == needleCharArray[0]) { //第一个指针对比,若相同则进入if语句。
23+
int j = i+1; //把此时的i+1赋值给k,直接比较第二个值
24+
int k = 1; //直接比较第二个值
25+
for (; k<needleCharArray.length && j<hayStackCharArray.length; k++) {
26+
if (hayStackCharArray[j] == needleCharArray[k])
27+
j++;
28+
else {
29+
break;
30+
}
31+
}
32+
if (needleCharArray.length==k){
33+
return i;
34+
}
35+
}
36+
}
37+
return -1;
38+
}
39+
40+
public static int strStrNew(String haystack, String needle){
41+
int hayStackLength = haystack.length();
42+
int needleLength = needle.length();
43+
if (needleLength>hayStackLength) return -1; if (needleLength == 0) return 0;
44+
for (int i = 0; i<hayStackLength-needleLength;i++) {
45+
if (haystack.substring(i,needleLength+i).equals(needle))
46+
return i;
47+
}
48+
return -1;
49+
}
50+
/**
51+
* 还应该尝试下kmp算法
52+
* @author:liuquan
53+
* @date:2018/11/28 22:00
54+
* @param
55+
* @return
56+
*/
57+
58+
}

0 commit comments

Comments
 (0)