forked from yubinbai/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
25 lines (21 loc) · 717 Bytes
/
Solution.java
File metadata and controls
25 lines (21 loc) · 717 Bytes
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
public class Solution {
public int strStr(String haystack, String needle) {
int needleLen = needle.length();
int haystackLen = haystack.length();
if (needleLen == 0) return 0;
if (haystackLen == 0) return -1;
for (int i = 0; i <= haystackLen - needleLen; i++) {
int k = i, j = 0;
while (j < needleLen && k < haystackLen && needle.charAt(j) == haystack.charAt(k)) {
j++;
k++;
if (j == needleLen) return i;
}
}
return -1;
}
public static void main(String[] args) {
Solution s = new Solution();
System.out.println(s.strStr("helllo", "ll"));
}
}