题目链接:https://leetcode.com/problems/implement-strstr/
题目:
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
算法:
1. public int strStr(String haystack, String needle) {
2. char h[] = haystack.toCharArray();
3. char n[] = needle.toCharArray();
4. int i = 0, j = 0;
5. while (i < h.length && j < n.length) {
6. if (h[i] == n[j]) {
7. j++;
8. i++;
9. else {
10. 1;
11. 0;
12. }
13.
14. }
15. if (j == n.length) {
16. return i - needle.length();
17. else {
18. return -1;
19. }
20. }