package com.company;
import com.sun.org.apache.xpath.internal.operations.Bool;
import java.util.*;
public class Main {
public static void main(String[] args) {
int t = strStr("fsadbutsad", "sad");
}
public static int strStr(String haystack, String needle) {
if (needle == null || needle.length() == 0 || haystack == null || haystack.length() == 0) {
return -1;
}
int neeleSize = needle.length();
int length = haystack.length();
int i = 0;
int j = neeleSize - 1;
if (length < neeleSize) {
return -1;
} else {
while (i <= length - neeleSize) {
String keystr = (String) haystack.substring(i, j+1);
if (!keystr.equals(needle)) {
i++;
j++;
} else {
return i;
}
}
}
return -1;
}
}
注:第一道不看思路,不看解法,完全自己敲出来的代码。