-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2352. 相等行列对.java
51 lines (45 loc) · 1.46 KB
/
2352. 相等行列对.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
int[][] grid1 = {
{3, 2, 1},
{1, 7, 6},
{2, 7, 7}
};
System.out.println(equalPairs(grid1)); // Output: 1
int[][] grid2 = {
{3, 1, 2, 2},
{1, 4, 4, 5},
{2, 4, 2, 2},
{2, 4, 2, 2}
};
System.out.println(equalPairs(grid2)); // Output: 3
}
public static int equalPairs(int[][] grid) {
int n = grid.length;
// Step 1: Store row vectors in a hash table
Map<String, Integer> rowVectors = new HashMap<>();
for (int[] row : grid) {
StringBuilder sb = new StringBuilder();
for (int num : row) {
sb.append(num).append(",");
}
String rowStr = sb.toString();
rowVectors.put(rowStr, rowVectors.getOrDefault(rowStr, 0) + 1);
}
// Step 2: Compare column vectors with stored row vectors
int count = 0;
for (int j = 0; j < n; j++) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(grid[i][j]).append(",");
}
String colStr = sb.toString();
if (rowVectors.containsKey(colStr)) {
count += rowVectors.get(colStr);
}
}
return count;
}
}