classSolution: defequalPairs(self, grid: List[List[int]]) -> int: res, n = 0, len(grid) for row inrange(n): for col inrange(n): if self.equal(row, col, n, grid): res += 1 return res defequal(self, row: int, col: int, n: int, grid: List[List[int]]) -> bool: for i inrange(n): if grid[row][i] != grid[i][col]: returnFalse returnTrue
classSolution { publicintequalPairs(int[][] grid) { intres=0, n = grid.length; for (introw=0; row < n; row++) { for (intcol=0; col < n; col++) { if (equal(row, col, n, grid)) { res++; } } } return res; }
publicbooleanequal(int row, int col, int n, int[][] grid) { for (inti=0; i < n; i++) { if (grid[row][i] != grid[i][col]) { returnfalse; } } returntrue; } }
publicclassSolution { publicintEqualPairs(int[][] grid) { int res = 0, n = grid.Length; for (int row = 0; row < n; row++) { for (int col = 0; col < n; col++) { if (Equal(row, col, n, grid)) { res++; } } } return res; }
publicboolEqual(int row, int col, int n, int[][] grid) { for (int i = 0; i < n; i++) { if (grid[row][i] != grid[i][col]) { returnfalse; } } returntrue; } }
classSolution { public: intequalPairs(vector<vector<int>>& grid){ int res = 0, n = grid.size(); for (int row = 0; row < n; row++) { for (int col = 0; col < n; col++) { if (equal(row, col, grid)) { res++; } } } return res; }
boolequal(int row, int col, vector<vector<int>>& grid){ int n = grid.size(); for (int i = 0; i < n; i++) { if (grid[row][i] != grid[i][col]) { returnfalse; } } returntrue; } };
boolequal(int row, int col, constint** grid, int gridSize) { for (int i = 0; i < gridSize; i++) { if (grid[row][i] != grid[i][col]) { returnfalse; } } returntrue; }
intequalPairs(int** grid, int gridSize, int* gridColSize) { int res = 0; for (int row = 0; row < gridSize; row++) { for (int col = 0; col < gridSize; col++) { if (equal(row, col, grid, gridSize)) { res++; } } } return res; }
classSolution: defequalPairs(self, grid: List[List[int]]) -> int: res, n = 0, len(grid) cnt = Counter(tuple(row) for row in grid) res = 0 for j inrange(n): res += cnt[tuple([grid[i][j] for i inrange(n)])] return res
intres=0; for (intj=0; j < n; j++) { List<Integer> arr = newArrayList<Integer>(); for (inti=0; i < n; i++) { arr.add(grid[i][j]); } if (cnt.containsKey(arr)) { res += cnt.get(arr); } } return res; } }
[sol2-Go]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
funcequalPairs(grid [][]int)int { n := len(grid) cnt := make(map[string]int) for _, row := range grid { cnt[fmt.Sprint(row)]++ } res := 0 for j := 0; j < n; j++ { var arr []int for i := 0; i < n; i++ { arr = append(arr, grid[i][j]) } if val, ok := cnt[fmt.Sprint(arr)]; ok { res += val } }