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

Commit3db72cd

Browse files
committed
Add solution and test-cases for problem 1254
1 parent5aa7d84 commit3db72cd

File tree

5 files changed

+91
-22
lines changed

5 files changed

+91
-22
lines changed

‎leetcode/1201-1300/1254.Number-of-Closed-Islands/README.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
#[1254.Number of Closed Islands][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+
Given a 2D`grid` consists of`0s` (land) and`1s` (water). An island is a maximal 4-directionally connected group of 0s and a closed island is an island**totally** (all left, top, right, bottom) surrounded by`1s`.
5+
6+
Return the number of closed islands.
77

8-
**Example 1:**
8+
**Example 1:**
9+
10+
![example1](./sample_3_1610.png)
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]]
14+
Output: 2
15+
Explanation:
16+
Islands in gray are closed because they are completely surrounded by water (group of 1s).
1317
```
1418

15-
##题意
16-
>...
19+
**Example 2:**
1720

18-
##题解
21+
![example2](./sample_4_1610.png)
1922

20-
###思路1
21-
>...
22-
Number of Closed Islands
23-
```go
23+
```
24+
Input: grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]
25+
Output: 1
2426
```
2527

28+
**Example 3:**
29+
30+
```
31+
Input: grid = [[1,1,1,1,1,1,1],
32+
[1,0,0,0,0,0,1],
33+
[1,0,1,1,1,0,1],
34+
[1,0,1,0,1,0,1],
35+
[1,0,1,1,1,0,1],
36+
[1,0,0,0,0,0,1],
37+
[1,1,1,1,1,1,1]]
38+
Output: 2
39+
```
2640

2741
##结语
2842

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,51 @@
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+
dir= [][]int{
9+
{1,0}, {0,1}, {-1,0}, {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]==1 {
17+
return
18+
}
19+
grid[x][y]=1
20+
for_,d:=rangedir {
21+
nx,ny:=d[0]+x,d[1]+y
22+
find(nx,ny)
23+
}
24+
}
25+
26+
fory:=0;y<cols;y++ {
27+
ifgrid[0][y]==0 {
28+
find(0,y)
29+
}
30+
ifgrid[rows-1][y]==0 {
31+
find(rows-1,y)
32+
}
33+
}
34+
forx:=1;x<rows-1;x++ {
35+
ifgrid[x][0]==0 {
36+
find(x,0)
37+
}
38+
ifgrid[x][cols-1]==0 {
39+
find(x,cols-1)
40+
}
41+
}
42+
forx:=1;x<rows-1;x++ {
43+
fory:=1;y<cols-1;y++ {
44+
ifgrid[x][y]==0 {
45+
ans++
46+
find(x,y)
47+
}
48+
}
49+
}
50+
returnans
551
}

‎leetcode/1201-1300/1254.Number-of-Closed-Islands/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+
{1,1,1,1,1,1,1,0},
18+
{1,0,0,0,0,1,1,0},
19+
{1,0,1,0,1,1,1,0},
20+
{1,0,0,0,0,1,0,1},
21+
{1,1,1,1,1,1,1,0},
22+
},2},
23+
{"TestCase2", [][]int{
24+
{0,0,1,0,0},
25+
{0,1,0,1,0},
26+
{0,1,1,1,0},
27+
},1},
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
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp