Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: Solution Js for lc no. 959 #3396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
yanglbme merged 15 commits into doocs:main from AE-Hertz:main
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
74c5827
feat: add solutions to lc problem: No.0885
AE-Hertz Aug 8, 2024
18cd1ac
Merge pull request #2 from AE-Hertz/AE-Hertz-patch-solution-LC--0885
AE-Hertz Aug 8, 2024
25caf40
style: format code and docs with prettier
AE-Hertz Aug 8, 2024
a759f9c
Update Solution.js
yanglbme Aug 8, 2024
4a49614
Update README.md
yanglbme Aug 8, 2024
549de73
Update README_EN.md
yanglbme Aug 8, 2024
56fbe9e
Merge branch 'doocs:main' into main
AE-Hertz Aug 9, 2024
a907914
feat: add solutions to lc project No. 0840
AE-Hertz Aug 9, 2024
5a244fc
Merge pull request #3 from AE-Hertz/AE-Hertz-patch-1
AE-Hertz Aug 9, 2024
6b698bb
Delete solution/0800-0899/0840.Magic Squares In Grid/Solution.js
AE-Hertz Aug 9, 2024
d7972c3
Merge branch 'doocs:main' into main
AE-Hertz Aug 10, 2024
cd8dc69
feat: Solution Js for lc no. 959
AE-Hertz Aug 10, 2024
074ba40
style: format code and docs with prettier
AE-Hertz Aug 10, 2024
1efac89
Update README.md
yanglbme Aug 10, 2024
d08b3b3
Update README_EN.md
yanglbme Aug 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions solution/0900-0999/0959.Regions Cut By Slashes/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,62 @@ func regionsBySlashes(grid []string) int {
}
```

#### JavaScript

```js
/**
* @param {string[]} grid
* @return {number}
*/

function regionsBySlashes(grid) {
const find = x => {
if (p[x] !== x) {
p[x] = find(p[x]);
}
return p[x];
};

const union = (a, b) => {
const pa = find(a);
const pb = find(b);
if (pa !== pb) {
p[pa] = pb;
size--;
}
};

const n = grid.length;
let size = n * n * 4;
const p = Array.from({ length: size }, (_, i) => i);

for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
const k = i * n + j;
if (i < n - 1) {
union(4 * k + 2, (k + n) * 4);
}
if (j < n - 1) {
union(4 * k + 1, (k + 1) * 4 + 3);
}
if (grid[i][j] === '/') {
union(4 * k, 4 * k + 3);
union(4 * k + 1, 4 * k + 2);
} else if (grid[i][j] === '\\') {
union(4 * k, 4 * k + 1);
union(4 * k + 2, 4 * k + 3);
} else {
union(4 * k, 4 * k + 1);
union(4 * k + 1, 4 * k + 2);
union(4 * k + 2, 4 * k + 3);
}
}
}

return size;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
56 changes: 56 additions & 0 deletions solution/0900-0999/0959.Regions Cut By Slashes/README_EN.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,62 @@ func regionsBySlashes(grid []string) int {
}
```

#### JavaScript

```js
/**
* @param {string[]} grid
* @return {number}
*/

function regionsBySlashes(grid) {
const find = x => {
if (p[x] !== x) {
p[x] = find(p[x]);
}
return p[x];
};

const union = (a, b) => {
const pa = find(a);
const pb = find(b);
if (pa !== pb) {
p[pa] = pb;
size--;
}
};

const n = grid.length;
let size = n * n * 4;
const p = Array.from({ length: size }, (_, i) => i);

for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
const k = i * n + j;
if (i < n - 1) {
union(4 * k + 2, (k + n) * 4);
}
if (j < n - 1) {
union(4 * k + 1, (k + 1) * 4 + 3);
}
if (grid[i][j] === '/') {
union(4 * k, 4 * k + 3);
union(4 * k + 1, 4 * k + 2);
} else if (grid[i][j] === '\\') {
union(4 * k, 4 * k + 1);
union(4 * k + 2, 4 * k + 3);
} else {
union(4 * k, 4 * k + 1);
union(4 * k + 1, 4 * k + 2);
union(4 * k + 2, 4 * k + 3);
}
}
}

return size;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
51 changes: 51 additions & 0 deletions solution/0900-0999/0959.Regions Cut By Slashes/Solution.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @param {string[]} grid
* @return {number}
*/

function regionsBySlashes(grid) {
const find = x => {
if (p[x] !== x) {
p[x] = find(p[x]);
}
return p[x];
};

const union = (a, b) => {
const pa = find(a);
const pb = find(b);
if (pa !== pb) {
p[pa] = pb;
size--;
}
};

const n = grid.length;
let size = n * n * 4;
const p = Array.from({ length: size }, (_, i) => i);

for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
const k = i * n + j;
if (i < n - 1) {
union(4 * k + 2, (k + n) * 4);
}
if (j < n - 1) {
union(4 * k + 1, (k + 1) * 4 + 3);
}
if (grid[i][j] === '/') {
union(4 * k, 4 * k + 3);
union(4 * k + 1, 4 * k + 2);
} else if (grid[i][j] === '\\') {
union(4 * k, 4 * k + 1);
union(4 * k + 2, 4 * k + 3);
} else {
union(4 * k, 4 * k + 1);
union(4 * k + 1, 4 * k + 2);
union(4 * k + 2, 4 * k + 3);
}
}
}

return size;
}

AltStyle によって変換されたページ (->オリジナル) /