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

Commit 58b297a

Browse files
Merge pull request youngyangyang04#2393 from Relsola/master
update 130.被围绕的区域,417.太平洋大西洋水流问题新增 JS 广搜和深搜解法
2 parents 6b0cc1d + 5c68af9 commit 58b297a

File tree

2 files changed

+237
-0
lines changed

2 files changed

+237
-0
lines changed

‎problems/0130.被围绕的区域.md‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,132 @@ class Solution:
435435

436436
```
437437

438+
### JavaScript
439+
```JavaScript
440+
/**
441+
* @description 深度搜索优先
442+
* @param {character[][]} board
443+
* @return {void} Do not return anything, modify board in-place instead.
444+
*/
445+
function solve(board) {
446+
const dir = [[-1, 0], [1, 0], [0, -1], [0, 1]];
447+
const [rowSize, colSize] = [board.length, board[0].length];
448+
449+
function dfs(board, x, y) {
450+
board[x][y] = 'A';
451+
for (let i = 0; i < 4; i++) {
452+
const nextX = dir[i][0] + x;
453+
const nextY = dir[i][1] + y;
454+
if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) {
455+
continue;
456+
}
457+
if (board[nextX][nextY] === 'O') {
458+
dfs(board, nextX, nextY);
459+
}
460+
}
461+
}
462+
463+
for (let i = 0; i < rowSize; i++) {
464+
if (board[i][0] === 'O') {
465+
dfs(board, i, 0);
466+
}
467+
if (board[i][colSize - 1] === 'O') {
468+
dfs(board, i, colSize - 1);
469+
}
470+
}
471+
472+
for (let i = 1; i < colSize - 1; i++) {
473+
if (board[0][i] === 'O') {
474+
dfs(board, 0, i);
475+
}
476+
if (board[rowSize - 1][i] === 'O') {
477+
dfs(board, rowSize - 1, i);
478+
}
479+
}
480+
481+
for (let i = 0; i < rowSize; i++) {
482+
for (let k = 0; k < colSize; k++) {
483+
if (board[i][k] === 'A') {
484+
board[i][k] = 'O';
485+
} else if (board[i][k] === 'O') {
486+
board[i][k] = 'X';
487+
}
488+
}
489+
}
490+
}
491+
492+
/**
493+
* @description 广度搜索优先
494+
* @param {character[][]} board
495+
* @return {void} Do not return anything, modify board in-place instead.
496+
*/
497+
function solve(board) {
498+
const dir = [[-1, 0], [1, 0], [0, -1], [0, 1]];
499+
const [rowSize, colSize] = [board.length, board[0].length];
500+
501+
function bfs(board, x, y) {
502+
board[x][y] = 'A';
503+
const stack = [y, x];
504+
505+
while (stack.length !== 0) {
506+
const top = [stack.pop(), stack.pop()];
507+
for (let i = 0; i < 4; i++) {
508+
const nextX = dir[i][0] + top[0];
509+
const nextY = dir[i][1] + top[1];
510+
511+
if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) {
512+
continue;
513+
}
514+
515+
if (board[nextX][nextY] === 'O') {
516+
board[nextX][nextY] = 'A';
517+
stack.push(nextY, nextX);
518+
}
519+
}
520+
}
521+
522+
for (let i = 0; i < 4; i++) {
523+
const nextX = dir[i][0] + x;
524+
const nextY = dir[i][1] + y;
525+
if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) {
526+
continue;
527+
}
528+
if (board[nextX][nextY] === 'O') {
529+
dfs(board, nextX, nextY);
530+
}
531+
}
532+
}
533+
534+
for (let i = 0; i < rowSize; i++) {
535+
if (board[i][0] === 'O') {
536+
bfs(board, i, 0);
537+
}
538+
if (board[i][colSize - 1] === 'O') {
539+
bfs(board, i, colSize - 1);
540+
}
541+
}
542+
543+
for (let i = 1; i < colSize - 1; i++) {
544+
if (board[0][i] === 'O') {
545+
bfs(board, 0, i);
546+
}
547+
if (board[rowSize - 1][i] === 'O') {
548+
bfs(board, rowSize - 1, i);
549+
}
550+
}
551+
552+
for (let i = 0; i < rowSize; i++) {
553+
for (let k = 0; k < colSize; k++) {
554+
if (board[i][k] === 'A') {
555+
board[i][k] = 'O';
556+
} else if (board[i][k] === 'O') {
557+
board[i][k] = 'X';
558+
}
559+
}
560+
}
561+
}
562+
```
563+
438564
<p align="center">
439565
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
440566
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

‎problems/0417.太平洋大西洋水流问题.md‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,117 @@ class Solution:
450450
return ans
451451
```
452452

453+
### JavaScript
454+
```JavaScript
455+
/**
456+
* @description 深度搜索优先
457+
* @param {number[][]} heights
458+
* @return {number[][]}
459+
*/
460+
function pacificAtlantic(heights) {
461+
const dir = [[-1, 0], [0, -1], [1, 0], [0, 1]];
462+
const [rowSize, colSize] = [heights.length, heights[0].length];
463+
const visited = Array.from({ length: rowSize }, _ =>
464+
Array.from({ length: colSize }, _ => new Array(2).fill(false))
465+
);
466+
const result = [];
467+
468+
function dfs(heights, visited, x, y, sign) {
469+
if (visited[x][y][sign]) {
470+
return;
471+
}
472+
visited[x][y][sign] = true;
473+
for (let i = 0; i < 4; i++) {
474+
const nextX = x + dir[i][0];
475+
const nextY = y + dir[i][1];
476+
if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) {
477+
continue;
478+
}
479+
if (heights[x][y] > heights[nextX][nextY]) {
480+
continue;
481+
}
482+
dfs(heights, visited, nextX, nextY, sign);
483+
}
484+
}
485+
486+
for (let i = 0; i < rowSize; i++) {
487+
dfs(heights, visited, i, 0, 0);
488+
dfs(heights, visited, i, colSize - 1, 1);
489+
}
490+
491+
for (let i = 0; i < colSize; i++) {
492+
dfs(heights, visited, 0, i, 0);
493+
dfs(heights, visited, rowSize - 1, i, 1);
494+
}
495+
496+
for (let i = 0; i < rowSize; i++) {
497+
for (let k = 0; k < colSize; k++) {
498+
if (visited[i][k][0] && visited[i][k][1]) {
499+
result.push([i, k]);
500+
}
501+
}
502+
}
503+
504+
return result;
505+
}
506+
507+
/**
508+
* @description 广度搜索优先
509+
* @param {number[][]} heights
510+
* @return {number[][]}
511+
*/
512+
function pacificAtlantic(heights) {
513+
const dir = [[-1, 0], [0, -1], [1, 0], [0, 1]];
514+
const [rowSize, colSize] = [heights.length, heights[0].length];
515+
const visited = Array.from({ length: rowSize }, _ =>
516+
Array.from({ length: colSize }, _ => new Array(2).fill(false))
517+
);
518+
const result = [];
519+
520+
function bfs(heights, visited, x, y, sign) {
521+
if (visited[x][y][sign]) {
522+
return;
523+
}
524+
visited[x][y][sign] = true;
525+
const stack = [y, x];
526+
while (stack.length !== 0) {
527+
[x, y] = [stack.pop(), stack.pop()];
528+
for (let i = 0; i < 4; i++) {
529+
const nextX = x + dir[i][0];
530+
const nextY = y + dir[i][1];
531+
if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) {
532+
continue;
533+
}
534+
if (heights[x][y] > heights[nextX][nextY] || visited[nextX][nextY][sign]) {
535+
continue;
536+
}
537+
visited[nextX][nextY][sign] = true;
538+
stack.push(nextY, nextX);
539+
}
540+
}
541+
}
542+
543+
for (let i = 0; i < rowSize; i++) {
544+
bfs(heights, visited, i, 0, 0);
545+
bfs(heights, visited, i, colSize - 1, 1);
546+
}
547+
548+
for (let i = 0; i < colSize; i++) {
549+
bfs(heights, visited, 0, i, 0);
550+
bfs(heights, visited, rowSize - 1, i, 1);
551+
}
552+
553+
for (let i = 0; i < rowSize; i++) {
554+
for (let k = 0; k < colSize; k++) {
555+
if (visited[i][k][0] && visited[i][k][1]) {
556+
result.push([i, k]);
557+
}
558+
}
559+
}
560+
561+
return result;
562+
}
563+
```
453564

454565

455566

0 commit comments

Comments
(0)

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