Easy Understanding Java Solution(DFS)


  • 0
    H
    public int numIslands(char[][] grid) {
        if(grid ==null||grid.length==0||grid[0].length==0)
        	return 0;
        boolean[][] visited = new boolean[grid.length][grid[0].length];
    	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' && !visited[i][j]){//land , not visited
    				markIsland(i,j,grid,visited);
    				count++;
    			}
    		}
    	}
    	return count;
    }
    public void markIsland(int i, int j, char[][] grid,boolean[][] visited){
    	visited[i][j]= true;
    	if(i-1>=0 && grid[i-1][j]=='1' &&!visited[i-1][j] )
    		markIsland(i-1,j,grid,visited);
    	if(i+1<visited.length && grid[i+1][j]=='1' && !visited[i+1][j])
    		markIsland(i+1,j,grid,visited);
    	if(j-1>=0 && grid[i][j-1]=='1'&& !visited[i][j-1])
    		markIsland(i,j-1,grid,visited);
    	if(j+1<visited[0].length && grid[i][j+1]=='1'&& !visited[i][j+1])
    		markIsland(i,j+1,grid,visited);
    }

Log in to reply
 

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