*LeetCode-Number of Islands

扫一遍就可以了 只要这个点是1 就开始explore它的neighbors 然后知道没有了 就返回 这时候主程序 count++ 记得要把explore过的点mark为0

public class Solution {
    public int numIslands(char[][] grid) {
        int count = 0;
        for ( int i = 0; i < grid.length; i ++ ){
            for ( int j = 0; j < grid[0].length; j ++ ){
                if ( grid[i][j] == '1'){
                    mark( grid, i, j );
                    count ++;
                }
            }
        }
        return count;
    }
    public void mark ( char [][] grid, int row, int col ){
        if ( row >= grid.length || row < 0 || col >= grid[0].length || col < 0 || grid[row][col] != '1')
            return;
        grid[row][col] = '0';
        mark( grid, row + 1, col );
        mark( grid, row, col + 1 );
        mark( grid, row - 1, col );
        mark( grid, row, col - 1 );
    }
}



你可能感兴趣的:(*LeetCode-Number of Islands)