匹配算法的错误答案[Java]
我遇到过这种匹配算法,它在整数数组中查找模式。它需要一个数组s,该数组具有进行匹配的序列。和另一个具有模式p的数组,该模式具有要匹配的模式匹配算法的错误答案[Java],java,matching,Java,Matching,我遇到过这种匹配算法,它在整数数组中查找模式。它需要一个数组s,该数组具有进行匹配的序列。和另一个具有模式p的数组,该模式具有要匹配的模式 // Example: Match {1, 2} in {1, 3, 1, 2, 3} => // after finding the first 3, skips ahead to the second 1, then finds {1, 2} at 2. public static int match2(final int[] s, final i
// Example: Match {1, 2} in {1, 3, 1, 2, 3} =>
// after finding the first 3, skips ahead to the second 1, then finds {1, 2} at 2.
public static int match2(final int[] s, final int[] p) {
for (int i = 0; i <= s.length - p.length; i++) {
int j;
for (j = 0; j < p.length; j++) {
if (t[i + j] != p[j]) {
i += j; // Mismatch, skip ahead.
break;
}
}
if (j == p.length) {
return i;
}
}
return -1;
}
是否有任何输入可以使这段代码发出错误的输出?它无法在{1,1,2}中找到{1,2}。NullPointerException是否算作错误输出?因为这很简单:只需将null传递给函数而不是数组。t[i+j]是否表示s[i+j]?