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 2b3ce44

Browse files
新增java解法
1 parent 1a75abd commit 2b3ce44

File tree

1 file changed

+124
-2
lines changed

1 file changed

+124
-2
lines changed

‎problems/1020.飞地的数量.md‎

Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,63 @@ public:
149149

150150
### Java
151151

152-
深度优先遍历版本:
152+
深度优先遍历(没有终止条件 + 空間優化(淹沒島嶼,沒有使用visited數組))
153+
```java
154+
//DFS
155+
class Solution {
156+
int count = 0;
157+
int[][] dir ={
158+
{0, 1},
159+
{1, 0},
160+
{-1, 0},
161+
{0, -1}
162+
};
163+
private void dfs(int[][] grid, int x, int y){
164+
if(grid[x][y] == 0)
165+
return;
166+
167+
grid[x][y] = 0;
168+
count++;
169+
170+
for(int i = 0; i < 4; i++){
171+
int nextX = x + dir[i][0];
172+
int nextY = y + dir[i][1];
173+
174+
if(nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length)
175+
continue;
176+
dfs(grid, nextX, nextY);
177+
}
178+
179+
}
180+
181+
public int numEnclaves(int[][] grid) {
182+
for(int i = 0; i < grid.length; i++){
183+
if(grid[i][0] == 1)
184+
dfs(grid, i, 0);
185+
if(grid[i][grid[0].length - 1] == 1)
186+
dfs(grid, i, grid[0].length - 1);
187+
}
188+
//初始化的時候,j 的上下限有調整過,必免重複操作。
189+
for(int j = 1; j < grid[0].length - 1; j++){
190+
if(grid[0][j] == 1)
191+
dfs(grid, 0, j);
192+
if(grid[grid.length - 1][j] == 1)
193+
dfs(grid, grid.length - 1, j);
194+
}
195+
count = 0;
196+
197+
for(int i = 1; i < grid.length - 1; i++){
198+
for(int j = 1; j < grid[0].length - 1; j++){
199+
if(grid[i][j] == 1)
200+
dfs(grid, i, j);
201+
}
202+
}
203+
return count;
204+
}
205+
}
206+
```
207+
208+
深度优先遍历(没有终止条件)
153209

154210
```java
155211
class Solution {
@@ -206,7 +262,7 @@ class Solution {
206262
}
207263
```
208264

209-
广度优先遍历版本:
265+
广度优先遍历(使用visited數組)
210266

211267
```java
212268
class Solution {
@@ -269,6 +325,72 @@ class Solution {
269325
}
270326
```
271327

328+
廣度优先遍历(空間優化(淹沒島嶼,沒有使用visited數組))
329+
```java
330+
//BFS
331+
class Solution {
332+
int count = 0;
333+
int[][] dir ={
334+
{0, 1},
335+
{1, 0},
336+
{-1, 0},
337+
{0, -1}
338+
};
339+
private void bfs(int[][] grid, int x, int y){
340+
Queue<Integer> que = new LinkedList<>();
341+
que.offer(x);
342+
que.offer(y);
343+
count++;
344+
grid[x][y] = 0;
345+
346+
while(!que.isEmpty()){
347+
int currX = que.poll();
348+
int currY = que.poll();
349+
350+
for(int i = 0; i < 4; i++){
351+
int nextX = currX + dir[i][0];
352+
int nextY = currY + dir[i][1];
353+
354+
if(nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length)
355+
continue;
356+
357+
if(grid[nextX][nextY] == 1){
358+
que.offer(nextX);
359+
que.offer(nextY);
360+
count++;
361+
grid[nextX][nextY] = 0;
362+
}
363+
}
364+
}
365+
}
366+
367+
public int numEnclaves(int[][] grid) {
368+
for(int i = 0; i < grid.length; i++){
369+
if(grid[i][0] == 1)
370+
bfs(grid, i, 0);
371+
if(grid[i][grid[0].length - 1] == 1)
372+
bfs(grid, i, grid[0].length - 1);
373+
}
374+
for(int j = 1; j < grid[0].length; j++){
375+
if(grid[0][j] == 1)
376+
bfs(grid, 0 , j);
377+
if(grid[grid.length - 1][j] == 1)
378+
bfs(grid, grid.length - 1, j);
379+
}
380+
count = 0;
381+
for(int i = 1; i < grid.length - 1; i++){
382+
for(int j = 1; j < grid[0].length - 1; j++){
383+
if(grid[i][j] == 1)
384+
bfs(grid,i ,j);
385+
}
386+
}
387+
return count;
388+
}
389+
}
390+
391+
392+
```
393+
272394
### Python
273395

274396
深度优先遍历

0 commit comments

Comments
(0)

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