Movatterモバイル変換


[0]ホーム

URL:


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

Commitc33b583

Browse files
authored
Merge pull request6boris#479 from 0xff-dev/master
Add solution and test-cases for problem 1020
2 parentsd159c85 +e8a400a commitc33b583

File tree

5 files changed

+78
-23
lines changed

5 files changed

+78
-23
lines changed

‎leetcode/1001-1100/1020.Number-of-Enclaves/README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
#[1020.Number of Enclaves][title]
22

3-
>[!WARNING|style:flat]
4-
>This question is temporarily unanswered if you have good ideas. Welcome to[Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
##Description
4+
You are given an`m x n` binary matrix`grid`, where`0` represents a sea cell and`1` represents a land cell.
5+
6+
A**move** consists of walking from one land cell to another adjacent (**4-directionally**) land cell or walking off the boundary of the`grid`.
7+
8+
Return the number of land cells in`grid` for which we cannot walk off the boundary of the grid in any number of**moves**.
79

8-
**Example 1:**
10+
**Example 1:**
11+
12+
![example1](./enclaves1.jpg)
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
16+
Output: 3
17+
Explanation: There are three 1s that are enclosed by 0s, and one 1 that is not enclosed because its on the boundary.
1318
```
1419

15-
##题意
16-
>...
20+
**Example 2:**
1721

18-
##题解
22+
![example2](./enclaves2.jpg)
1923

20-
###思路1
21-
>...
22-
Number of Enclaves
23-
```go
2424
```
25-
25+
Input: grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
26+
Output: 0
27+
Explanation: All 1s are either on the boundary or can reach the boundary.
28+
```
2629

2730
##结语
2831

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,48 @@
11
package Solution
22

3-
funcSolution(xbool)bool {
4-
returnx
3+
funcSolution(grid [][]int)int {
4+
ans:=0
5+
rows,cols:=len(grid),len(grid[0])
6+
var (
7+
findfunc(int,int)
8+
dirs= [][]int{
9+
{1,0}, {-1,0}, {0,1}, {0,-1},
10+
}
11+
)
12+
find=func(x,yint) {
13+
ifx<0||x>=rows||y<0||y>=cols {
14+
return
15+
}
16+
ifgrid[x][y]==0 {
17+
return
18+
}
19+
grid[x][y]=0
20+
for_,dir:=rangedirs {
21+
find(dir[0]+x,dir[1]+y)
22+
}
23+
}
24+
forcol:=0;col<cols;col++ {
25+
ifgrid[0][col]==1 {
26+
find(0,col)
27+
}
28+
ifgrid[rows-1][col]==1 {
29+
find(rows-1,col)
30+
}
31+
}
32+
forrow:=1;row<rows-1;row++ {
33+
ifgrid[row][0]==1 {
34+
find(row,0)
35+
}
36+
ifgrid[row][cols-1]==1 {
37+
find(row,cols-1)
38+
}
39+
}
40+
forrow:=1;row<rows-1;row++ {
41+
forcol:=1;col<cols-1;col++ {
42+
ifgrid[row][col]==1 {
43+
ans++
44+
}
45+
}
46+
}
47+
returnans
548
}

‎leetcode/1001-1100/1020.Number-of-Enclaves/Solution_test.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@ func TestSolution(t *testing.T) {
1010
//测试用例
1111
cases:= []struct {
1212
namestring
13-
inputsbool
14-
expectbool
13+
inputs[][]int
14+
expectint
1515
}{
16-
{"TestCase",true,true},
17-
{"TestCase",true,true},
18-
{"TestCase",false,false},
16+
{"TestCase1", [][]int{
17+
{0,0,0,0},
18+
{1,0,1,0},
19+
{0,1,1,0},
20+
{0,0,0,0},
21+
},3},
22+
{"TestCase2", [][]int{
23+
{0,1,1,0},
24+
{0,0,1,0},
25+
{0,0,1,0},
26+
{0,0,0,0},
27+
},0},
1928
}
2029

2130
//开始测试
@@ -30,10 +39,10 @@ func TestSolution(t *testing.T) {
3039
}
3140
}
3241

33-
//压力测试
42+
//压力测试
3443
funcBenchmarkSolution(b*testing.B) {
3544
}
3645

37-
//使用案列
46+
//使用案列
3847
funcExampleSolution() {
3948
}
Loading
Loading

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp