|
42 | 42 |
|
43 | 43 | ## Solutions
|
44 | 44 |
|
45 |
| -### Solution 1 |
| 45 | +### Solution 1: Simulation |
46 | 46 |
|
47 |
| -<!-- tabs:start --> |
48 |
| - |
49 |
| -```python |
50 |
| -class Solution: |
51 |
| - def equalPairs(self, grid: List[List[int]]) -> int: |
52 |
| - g = [list(col) for col in zip(*grid)] |
53 |
| - return sum(row == col for row in grid for col in g) |
54 |
| -``` |
55 |
| - |
56 |
| -```java |
57 |
| -class Solution { |
58 |
| - public int equalPairs(int[][] grid) { |
59 |
| - int n = grid.length; |
60 |
| - int[][] g = new int[n][n]; |
61 |
| - for (int j = 0; j < n; ++j) { |
62 |
| - for (int i = 0; i < n; ++i) { |
63 |
| - g[i][j] = grid[j][i]; |
64 |
| - } |
65 |
| - } |
66 |
| - int ans = 0; |
67 |
| - for (var row : grid) { |
68 |
| - for (var col : g) { |
69 |
| - int ok = 1; |
70 |
| - for (int i = 0; i < n; ++i) { |
71 |
| - if (row[i] != col[i]) { |
72 |
| - ok = 0; |
73 |
| - break; |
74 |
| - } |
75 |
| - } |
76 |
| - ans += ok; |
77 |
| - } |
78 |
| - } |
79 |
| - return ans; |
80 |
| - } |
81 |
| -} |
82 |
| -``` |
83 |
| - |
84 |
| -```cpp |
85 |
| -class Solution { |
86 |
| -public: |
87 |
| - int equalPairs(vector<vector<int>>& grid) { |
88 |
| - int n = grid.size(); |
89 |
| - vector<vector<int>> g(n, vector<int>(n)); |
90 |
| - for (int j = 0; j < n; ++j) { |
91 |
| - for (int i = 0; i < n; ++i) { |
92 |
| - g[i][j] = grid[j][i]; |
93 |
| - } |
94 |
| - } |
95 |
| - int ans = 0; |
96 |
| - for (auto& row : grid) { |
97 |
| - for (auto& col : g) { |
98 |
| - ans += row == col; |
99 |
| - } |
100 |
| - } |
101 |
| - return ans; |
102 |
| - } |
103 |
| -}; |
104 |
| -``` |
105 |
| -
|
106 |
| -```go |
107 |
| -func equalPairs(grid [][]int) (ans int) { |
108 |
| - n := len(grid) |
109 |
| - g := make([][]int, n) |
110 |
| - for i := range g { |
111 |
| - g[i] = make([]int, n) |
112 |
| - for j := 0; j < n; j++ { |
113 |
| - g[i][j] = grid[j][i] |
114 |
| - } |
115 |
| - } |
116 |
| - for _, row := range grid { |
117 |
| - for _, col := range g { |
118 |
| - ok := 1 |
119 |
| - for i, v := range row { |
120 |
| - if v != col[i] { |
121 |
| - ok = 0 |
122 |
| - break |
123 |
| - } |
124 |
| - } |
125 |
| - ans += ok |
126 |
| - } |
127 |
| - } |
128 |
| - return |
129 |
| -} |
130 |
| -``` |
131 |
| - |
132 |
| -```ts |
133 |
| -function equalPairs(grid: number[][]): number { |
134 |
| - const n = grid.length; |
135 |
| - const g = Array.from({ length: n }, () => Array.from({ length: n }, () => 0)); |
136 |
| - for (let j = 0; j < n; ++j) { |
137 |
| - for (let i = 0; i < n; ++i) { |
138 |
| - g[i][j] = grid[j][i]; |
139 |
| - } |
140 |
| - } |
141 |
| - let ans = 0; |
142 |
| - for (const row of grid) { |
143 |
| - for (const col of g) { |
144 |
| - ans += Number(row.toString() === col.toString()); |
145 |
| - } |
146 |
| - } |
147 |
| - return ans; |
148 |
| -} |
149 |
| -``` |
150 |
| - |
151 |
| -<!-- tabs:end --> |
| 47 | +We directly compare each row and column of the matrix $grid$. If they are equal, then it is a pair of equal row-column pairs, and we increment the answer by one. |
152 | 48 |
|
153 |
| -### Solution 2 |
| 49 | +The time complexity is $O(n^3)$, where $n$ is the number of rows or columns in the matrix $grid$. The space complexity is $O(1)$. |
154 | 50 |
|
155 | 51 | <!-- tabs:start -->
|
156 | 52 |
|
|
0 commit comments