解法
只用统计有多少对,很明显应该用哈希表存储所有的行,在通过列一一比较,统计出结果.
code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
var equalPairs = function (grid) { const l = grid.length; const cnt = {}; grid.forEach((element) => { let s = element.toString(); cnt[s] = (cnt[s] || 0) + 1; }); let res = 0; for (let i = 0; i < l; i++) { const arr = []; for (let j = 0; j < l; j++) { arr.push(grid[j][i]); } const arrStr = arr.toString(); if (cnt[arrStr]) { res += cnt[arrStr]; } } return res; };
|