岛屿数量
深度优先遍历
class Solution { public int numIslands(char[][] grid) { int xlen = grid[0].length; int ylen = grid.length; int count = 0; for (int x = 0; x < xlen; x++) { for (int y = 0; y < ylen; y++) { if (grid[y][x] == '1') { dfs(grid, y, x); count++; } } } return count; } private void dfs(char[][] grid, int y, int x) { if (y < 0 || y >= grid.length) return; if (x < 0 || x >= grid[0].length) return; if (grid[y][x] == '0') return; grid[y][x] = '0'; int[] dx = { 0, 0, 1, -1 }; int[] dy = { 1, -1, 0, 0 }; for (int i = 0; i < 4; i++) { dfs(grid, y + dy[i], x + dx[i]); } } }
标签:优先,遍历,return,int,dfs,++,length,grid,leetCode From: https://www.cnblogs.com/zrzct/p/18508600