JAVA-------------Easy Version To Understand!!!!!!!!!!


  • 8
    H
    	public static void DFS(char[][] grid, int i, int j) {
    	if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length)
    		return;
    	if (grid[i][j] == '1') {
    		grid[i][j] = '2';
    		DFS(grid, i + 1, j);
    		DFS(grid, i - 1, j);
    		DFS(grid, i, j + 1);
    		DFS(grid, i, j - 1);
    	}
    }
    
    public static int numIslands(char[][] grid) {
    	if (grid == null || grid.length == 0)
    		return 0;
    	int row = grid.length, column = grid[0].length, count = 0;
    	for (int i = 0; i < row; i++)
    		for (int j = 0; j < column; j++) {
    			if (grid[i][j] == '1') {
    				count++;
    				DFS(grid, i, j);
    			}
    		}
    	return count;
    }

  • 1
    H

    I like the "very easy to understand series" :)


  • 0
    H

    thank you ~~


  • 0
    A

    Can you explain logic behing setting cell as '2' after you visited ?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.