Skip to content

Commit 129c43f

Browse files
committed
feat: add solutions to lc problem: No.1020
No.1020.Number of Enclaves
1 parent 43a6f65 commit 129c43f

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

solution/1000-1099/1020.Number of Enclaves/README.md

+78
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,84 @@ func numEnclaves(grid [][]int) int {
424424
}
425425
```
426426

427+
### **TypeScript**
428+
429+
```ts
430+
function numEnclaves(grid: number[][]): number {
431+
let res = 0;
432+
const m = grid.length;
433+
const n = grid[0].length;
434+
const dfs = (y: number, x: number) => {
435+
if (x < 0 || x >= n || y < 0 || y >= m || grid[y][x] === 0) {
436+
return;
437+
}
438+
grid[y][x] = 0;
439+
dfs(y + 1, x);
440+
dfs(y, x + 1);
441+
dfs(y - 1, x);
442+
dfs(y, x - 1);
443+
};
444+
for (let i = 0; i < n; i++) {
445+
dfs(0, i);
446+
dfs(m - 1, i);
447+
}
448+
for (let i = 0; i < m; i++) {
449+
dfs(i, 0);
450+
dfs(i, n - 1);
451+
}
452+
for (let i = 1; i < m - 1; i++) {
453+
for (let j = 1; j < n - 1; j++) {
454+
if (grid[i][j] === 1) {
455+
res++;
456+
}
457+
}
458+
}
459+
return res;
460+
}
461+
```
462+
463+
### **Rust**
464+
465+
```rust
466+
impl Solution {
467+
fn dfs(grid: &mut Vec<Vec<i32>>, y: usize, x: usize) {
468+
if y >= grid.len() || x >= grid[0].len() || grid[y][x] == 0 {
469+
return;
470+
}
471+
grid[y][x] = 0;
472+
Solution::dfs(grid, y + 1, x);
473+
Solution::dfs(grid, y, x + 1);
474+
if y != 0 {
475+
Solution::dfs(grid, y - 1, x);
476+
}
477+
if x != 0 {
478+
Solution::dfs(grid, y, x - 1);
479+
}
480+
}
481+
pub fn num_enclaves(mut grid: Vec<Vec<i32>>) -> i32 {
482+
let mut res = 0;
483+
let m = grid.len();
484+
let n = grid[0].len();
485+
for i in 0..m {
486+
Solution::dfs(&mut grid, i, 0);
487+
Solution::dfs(&mut grid, i, n - 1);
488+
}
489+
for i in 0..n {
490+
Solution::dfs(&mut grid, 0, i);
491+
Solution::dfs(&mut grid, m - 1, i);
492+
}
493+
for i in 1..m - 1 {
494+
for j in 1..n - 1 {
495+
if grid[i][j] == 1 {
496+
res += 1;
497+
}
498+
}
499+
}
500+
res
501+
}
502+
}
503+
```
504+
427505
### **...**
428506

429507
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
impl Solution {
2+
fn dfs(grid: &mut Vec<Vec<i32>>, y: usize, x: usize) {
3+
if y >= grid.len() || x >= grid[0].len() || grid[y][x] == 0 {
4+
return;
5+
}
6+
grid[y][x] = 0;
7+
Solution::dfs(grid, y + 1, x);
8+
Solution::dfs(grid, y, x + 1);
9+
if y != 0 {
10+
Solution::dfs(grid, y - 1, x);
11+
}
12+
if x != 0 {
13+
Solution::dfs(grid, y, x - 1);
14+
}
15+
}
16+
pub fn num_enclaves(mut grid: Vec<Vec<i32>>) -> i32 {
17+
let mut res = 0;
18+
let m = grid.len();
19+
let n = grid[0].len();
20+
for i in 0..m {
21+
Solution::dfs(&mut grid, i, 0);
22+
Solution::dfs(&mut grid, i, n - 1);
23+
}
24+
for i in 0..n {
25+
Solution::dfs(&mut grid, 0, i);
26+
Solution::dfs(&mut grid, m - 1, i);
27+
}
28+
for i in 1..m - 1 {
29+
for j in 1..n - 1 {
30+
if grid[i][j] == 1 {
31+
res += 1;
32+
}
33+
}
34+
}
35+
res
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function numEnclaves(grid: number[][]): number {
2+
let res = 0;
3+
const m = grid.length;
4+
const n = grid[0].length;
5+
const dfs = (y: number, x: number) => {
6+
if (x < 0 || x >= n || y < 0 || y >= m || grid[y][x] === 0) {
7+
return;
8+
}
9+
grid[y][x] = 0;
10+
dfs(y + 1, x);
11+
dfs(y, x + 1);
12+
dfs(y - 1, x);
13+
dfs(y, x - 1);
14+
};
15+
for (let i = 0; i < n; i++) {
16+
dfs(0, i);
17+
dfs(m - 1, i);
18+
}
19+
for (let i = 0; i < m; i++) {
20+
dfs(i, 0);
21+
dfs(i, n - 1);
22+
}
23+
for (let i = 1; i < m - 1; i++) {
24+
for (let j = 1; j < n - 1; j++) {
25+
if (grid[i][j] === 1) {
26+
res++;
27+
}
28+
}
29+
}
30+
return res;
31+
}

0 commit comments

Comments
 (0)