LC.P2352[相等行列对]

方法一:暴力

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int equalPairs(int[][] grid) {
int n = grid.length, ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (isEqual(i, j, n, grid)) ++ans;
}
}
return ans;
}

private boolean isEqual(int row, int col, int n, int[][] grid) {
for (int i = 0; i < n; ++i) {
if (grid[row][i] != grid[i][col]) return false;
}
return true;
}
}
  • 时间复杂度:$O(n^3)$
  • 空间复杂度:$O(1)$