解题思路:按个比较,找出第一个一样的。
class Solution {
public int strStr(String haystack, String needle) {
if (needle.length() == 0) {
return 0;
}
for (int i = 0; i <= (haystack.length() - needle.length()); i++) {
if (haystack.charAt(i) == needle.charAt(0)) {
for (int j = 0; j < needle.length(); j++) {
if (haystack.charAt(i + j) != needle.charAt(j)) {
break;
} else {
if (j == needle.length() - 1) {
return i;
}
}
}
}
}
return -1;
}
}