leetcode 0028
说明
只是为了记录一下,不求多快,也不深究。
会简要描述思路,代码中不写注释。
如碰到不会做的用了别人代码会在博客中标出。
题目描述
思路
注意一下边界条件。
一个一个比过去就行了。
class Solution {
public boolean isSame(String haystack, int start, int end, String needle) {
for (int i = start, j = 0; i < end; i++, j++) {
if (haystack.charAt(i) != needle.charAt(j)) {
return false;
}
}
return true;
}
public int strStr(String haystack, String needle) {
if (needle.length() == 0) {
return 0;
}
if (haystack.length() < needle.length()) {
return -1;
}
for (int i = 0; i <= haystack.length() - needle.length(); i++) {
if (!isSame(haystack, i, i + needle.length(), needle)) {
continue;
} else {
return i;
}
}
return -1;
}
}