-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathlc28.java
35 lines (34 loc) · 1.03 KB
/
lc28.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package code;
/*
* 28. Implement strStr()
* 题意:找出子串在给定字符串的起始位置
* 难度:Easy
* 分类:Two Pointers, String
* Tips:注意判断子串为空的方法为needle.length()==0,不要用needle==""
* 最优的解法应该是O(N)的,类似KMP的思路,不过面试不会让写KMP的
*/
public class lc28 {
public static void main(String[] args) {
String haystack = "aaaaa";
String needle = "ab";
System.out.println(strStr(haystack, needle));
}
public static int strStr(String haystack, String needle) {
if(needle.length()==0)
return 0;
int cur1 = 0, cur2 = 0;
while(cur1<haystack.length()){
if(haystack.charAt(cur1)==needle.charAt(cur2)){
cur1++;
cur2++;
if(cur2==needle.length())
return cur1-cur2;
}else{
cur1++;
cur1 = cur1-cur2;
cur2 = 0;
}
}
return -1;
}
}