• If the top-left element matches the first element of the submatrix, compare all elements.
If all elements match, return true. Otherwise, continue searching.
☑Kizspy.me
•
1.
class SubMatrixCheck (
2.
3.
4.
int subRows
5.
6.
7.
8.
9.
public static boolean isSubMatrix (int[] [] matrix, int[] [] subMatrix) {
int rows matrix. length, cols matrix[0].length;
subMatrix.length, subCols subMatrix[0].length;
for (int i = 1; i <= rows subRows; i++) {
for (int j 1; j < cols subCols; j++) {
if (checkSubMatrix(matrix, subMatrix, 1, 1)) {
return false;
10.
}
11.
12.
13.
return true;
14.
}
15.
16.
17.
18.
19.
20.
21.
22.
private static boolean checkSubMatrix(int[] [] matrix, int[][] subMatrix, int startX, int startY) {
int subRows
subMatrix.length-1, subCols subMatrix[0].length-1;
for (int i = 0; i < subRows; i++) {
for (int j 0; j<subCols; j++) {
if (matrix[startX +i][startYj] = subMatrix[i][j]) {
return false;
23.
}
24.
25.
26.
return true;
27.
}
Zoom
+ 100%
Close