提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 一、力扣200. 岛屿数量
- 二、力扣695. 岛屿的最大面积
- 三、力扣1020. 飞地的数量
- 四、力扣130. 被围绕的区域
前言
依然是从地图周边出发,将周边空格相邻的'O'都做上标记,然后在遍历一遍地图,遇到 'O' 且没做过标记的,那么都是地图中间的'O',全部改成'X'就行。
一、力扣200. 岛屿数量
class Solution { int[][] arr = new int[][]{{0,1},{0,-1},{-1,0},{1,0}}; boolean[][] flag; public int numIslands(char[][] grid) { int m = grid.length, n = grid[0].length; int res = 0; flag = new boolean[m][n]; for(int i = 0; i < m; i ++){ for(int j = 0; j < n; j ++){ if(!flag[i][j] && grid[i][j] == '1'){ res ++; bfs(grid, i, j); } } } return res; } public void bfs(char[][] grid, int x, int y){ Deque
deq = new LinkedList<>(); deq.offerLast(new int[]{x,y}); while(!deq.isEmpty()){ int size = deq.size(); for(int i = 0; i < size; i ++){ int[] cur = deq.pollFirst(); for(int j = 0; j < 4; j ++){ int curX = cur[0] + arr[j][0]; int curY = cur[1] + arr[j][1]; if(curX < 0 || curX >= grid.length || curY < 0 || curY >= grid[0].length){ continue; } if(!flag[curX][curY] && grid[curX][curY] == '1'){ flag[curX][curY] = true; deq.offerLast(new int[]{curX,curY}); } } } } } } 二、力扣695. 岛屿的最大面积
class Solution { int res = 0; int count = 0; int[][] arr = new int[][]{{0,1},{0,-1},{-1,0},{1,0}}; boolean[][] flag; public int maxAreaOfIsland(int[][] grid) { int m = grid.length, n = grid[0].length; flag = new boolean[m][n]; for(int i = 0; i < m; i ++){ for(int j = 0; j < n; j ++){ if(!flag[i][j] && grid[i][j] == 1){ flag[i][j] = true; count = 1; dfs(grid,i,j); } } } return res; } public void dfs(int[][] grid, int x, int y){ res = Math.max(count,res); for(int i = 0; i < 4; i ++){ int curX = x + arr[i][0]; int curY = y + arr[i][1]; if(curX < 0 || curX >= grid.length || curY < 0 || curY >= grid[0].length){ continue; } if(!flag[curX][curY] && grid[curX][curY] == 1){ flag[curX][curY] = true; count ++; dfs(grid,curX,curY); } } } }
三、力扣1020. 飞地的数量
class Solution { int res = 0; int count = 0; int[][] arr = new int[][]{{0,1},{0,-1},{-1,0},{1,0}}; boolean[][] flag; boolean f; public int numEnclaves(int[][] grid) { int m = grid.length, n = grid[0].length; flag = new boolean[m][n]; for(int i = 0; i < m; i ++){ for(int j = 0; j < n; j ++){ if(!flag[i][j] && grid[i][j] == 1){ flag[i][j] = true; count = 1; f = false; dfs(grid,i,j); if(!f){ res += count; } } } } return res; } public void dfs(int[][] grid, int x, int y){ for(int i = 0; i < 4; i ++){ int curX = x + arr[i][0]; int curY = y + arr[i][1]; if(curX < 0 || curX >= grid.length || curY < 0 || curY >= grid[0].length){ f = true; continue; } if(!flag[curX][curY] && grid[curX][curY] == 1){ count ++; flag[curX][curY] = true; dfs(grid,curX,curY); } } } }
四、力扣130. 被围绕的区域
class Solution { int[][] arr = new int[][]{{0,1},{0,-1},{-1,0},{1,0}}; boolean[][] flag; public void solve(char[][] board) { int m = board.length, n = board[0].length; flag = new boolean[m][n]; for(int i = 0; i < m; i ++){ if(!flag[i][0] && board[i][0] == 'O'){ flag[i][0] = true; dfs(board, i, 0); } if(!flag[i][n-1] && board[i][n-1] == 'O'){ flag[i][n-1] = true; dfs(board,i,n-1); } } for(int i = 0; i < n; i ++){ if(!flag[0][i] && board[0][i] == 'O'){ flag[0][i] = true; dfs(board,0,i); } if(!flag[m-1][i] && board[m-1][i] == 'O'){ flag[m-1][i] = true; dfs(board,m-1,i); } } for(int i = 0; i < m; i ++){ for(int j = 0; j < n; j ++){ if(!flag[i][j] && board[i][j] == 'O'){ board[i][j] = 'X'; } } } } public void dfs(char[][] board, int x, int y){ for(int i = 0; i < 4; i++){ int curX = x + arr[i][0]; int curY = y + arr[i][1]; if(curX < 0 || curX >= board.length || curY < 0 || curY >= board[0].length){ continue; } if(!flag[curX][curY] && board[curX][curY] == 'O'){ flag[curX][curY] = true; dfs(board,curX,curY); } } } }
还没有评论,来说两句吧...