第一百零八天:力扣959题,由斜杠划分区域
地址:leetcode-cn.com/problems/re…
思路:并查集
var regionsBySlashes = function(grid) {
const n = grid.length;
const f = new Array(n * n * 4).fill(0)
.map((element, index) => index);
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
const idx = i * n + j;
if (i < n - 1) {
const bottom = idx + n;
merge(f, idx * 4 + 2, bottom * 4);
}
if (j < n - 1) {
const right = idx + 1;
merge(f, idx * 4 + 1, right * 4 + 3);
}
if (grid[i][j] === '/') {
merge(f, idx * 4, idx * 4 + 3);
merge(f, idx * 4 + 1, idx * 4 + 2);
} else if (grid[i].charAt(j) == '\\') {
merge(f, idx * 4, idx * 4 + 1);
merge(f, idx * 4 + 2, idx * 4 + 3);
} else {
merge(f, idx * 4, idx * 4 + 1);
merge(f, idx * 4 + 1, idx * 4 + 2);
merge(f, idx * 4 + 2, idx * 4 + 3);
}
}
}
const fathers = new Set();
for (let i = 0; i < n * n * 4; i++) {
const fa = find(f, i);
fathers.add(fa);
}
return [...fathers].length;
};
find = (f, x) => {
if (f[x] === x) {
return x;
}
const fa = find(f, f[x]);
f[x] = fa;
return fa;
}
merge = (f, x, y) => {
const fx = find(f, x);
const fy = find(f, y);
f[fx] = fy;
}
执行用时:136 ms, 在所有 JavaScript 提交中击败了38.46%的用户
内存消耗:44.5 MB, 在所有 JavaScript 提交中击败了7.69%的用户