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 b1292ee

Browse files
authored
feat: add js solution to lc problem: No.0079 (doocs#3233)
1 parent 5b768cd commit b1292ee

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

‎solution/0000-0099/0079.Word Search/README.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,42 @@ function exist(board: string[][], word: string): boolean {
264264
}
265265
```
266266

267+
#### JavaScript
268+
269+
```js
270+
function exist(board, word) {
271+
const [m, n] = [board.length, board[0].length];
272+
const dirs = [-1, 0, 1, 0, -1];
273+
const dfs = (i, j, k) => {
274+
if (k === word.length - 1) {
275+
return board[i][j] === word[k];
276+
}
277+
if (board[i][j] !== word[k]) {
278+
return false;
279+
}
280+
const c = board[i][j];
281+
board[i][j] = '0';
282+
for (let u = 0; u < 4; ++u) {
283+
const [x, y] = [i + dirs[u], j + dirs[u + 1]];
284+
const ok = x >= 0 && x < m && y >= 0 && y < n;
285+
if (ok && board[x][y] !== '0' && dfs(x, y, k + 1)) {
286+
return true;
287+
}
288+
}
289+
board[i][j] = c;
290+
return false;
291+
};
292+
for (let i = 0; i < m; ++i) {
293+
for (let j = 0; j < n; ++j) {
294+
if (dfs(i, j, 0)) {
295+
return true;
296+
}
297+
}
298+
}
299+
return false;
300+
}
301+
```
302+
267303
#### Rust
268304

269305
```rust

‎solution/0000-0099/0079.Word Search/README_EN.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,42 @@ function exist(board: string[][], word: string): boolean {
261261
}
262262
```
263263

264+
#### JavaScript
265+
266+
```js
267+
function exist(board, word) {
268+
const [m, n] = [board.length, board[0].length];
269+
const dirs = [-1, 0, 1, 0, -1];
270+
const dfs = (i, j, k) => {
271+
if (k === word.length - 1) {
272+
return board[i][j] === word[k];
273+
}
274+
if (board[i][j] !== word[k]) {
275+
return false;
276+
}
277+
const c = board[i][j];
278+
board[i][j] = '0';
279+
for (let u = 0; u < 4; ++u) {
280+
const [x, y] = [i + dirs[u], j + dirs[u + 1]];
281+
const ok = x >= 0 && x < m && y >= 0 && y < n;
282+
if (ok && board[x][y] !== '0' && dfs(x, y, k + 1)) {
283+
return true;
284+
}
285+
}
286+
board[i][j] = c;
287+
return false;
288+
};
289+
for (let i = 0; i < m; ++i) {
290+
for (let j = 0; j < n; ++j) {
291+
if (dfs(i, j, 0)) {
292+
return true;
293+
}
294+
}
295+
}
296+
return false;
297+
}
298+
```
299+
264300
#### Rust
265301

266302
```rust
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function exist(board, word) {
2+
const [m, n] = [board.length, board[0].length];
3+
const dirs = [-1, 0, 1, 0, -1];
4+
const dfs = (i, j, k) => {
5+
if (k === word.length - 1) {
6+
return board[i][j] === word[k];
7+
}
8+
if (board[i][j] !== word[k]) {
9+
return false;
10+
}
11+
const c = board[i][j];
12+
board[i][j] = '0';
13+
for (let u = 0; u < 4; ++u) {
14+
const [x, y] = [i + dirs[u], j + dirs[u + 1]];
15+
const ok = x >= 0 && x < m && y >= 0 && y < n;
16+
if (ok && board[x][y] !== '0' && dfs(x, y, k + 1)) {
17+
return true;
18+
}
19+
}
20+
board[i][j] = c;
21+
return false;
22+
};
23+
for (let i = 0; i < m; ++i) {
24+
for (let j = 0; j < n; ++j) {
25+
if (dfs(i, j, 0)) {
26+
return true;
27+
}
28+
}
29+
}
30+
return false;
31+
}

0 commit comments

Comments
(0)

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