classSolution1{// 1. scan grid 2.if ==1, dfs and label
final static int[][] dirs ={{1,0},{-1,0},{0,1},{0,-1}};public int numIslands(char[][] grid){// sanity test if(grid ==null|| grid.length ==0|| grid[0].length ==0){return0;}
int count =0;
final int rows = grid.length;
final int cols = grid[0].length;for(int i=0; i<rows; i++){for(int j=0; j<cols; j++){if(grid[i][j]=='1'){
count++;// System.out.print(count);dfs(grid, i, j, rows, cols);}}}return count;}privatevoiddfs(char[][] grid, int x, int y, int rows, int cols){// basic caseif(x <0|| x >= rows || y <0|| y >= cols || grid[x][y]=='0'|| grid[x][y]=='2'){return;}// recursive rules
grid[x][y]='2';for(int[] dir: dirs){
int neiX = x + dir[0];
int neiY = y + dir[1];dfs(grid, neiX, neiY, rows, cols);}}}
Problem:Analysisscan grid to find ‘1’if ‘1’, then dfs or bfs and labelCodeclass Solution1 { // 1. scan grid 2.if ==1, dfs and label final static int[][] dirs = {{1,0},{-1,0},{0,1},...