2

题目要求

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. 
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. 
You may assume all four edges of the grid are all surrounded by water.

Example 1:

11110
11010
11000
00000
Answer: 1

Example 2:

11000
11000
00100
00011
Answer: 3

提供一个二维数组表示一张地图,其中1代表陆地,0代表海洋。问在这张地图上一共有几个陆地.

思路一: union-find并查集

这道题目从经典的数据结构的角度来说可以使用并查集来进行判断,将每一个海洋看做一个集合合并起来,将相邻的陆地通过并查集连接起来。最后查看并查集中剩余下的集合数。
这里使用一个新的二维数组来表示对应地图上的元素属于哪个并查集。在合并的时候先进行判断,如果二者为已经相连的陆地,则无需合并,否则将新的二维数组上的元素指向所在的并查集。

int row;
    int column;
    char[][] grid;
    int count;
    int[][] tempRegion;
    public int numIslands(char[][] grid) {
        if(grid==null || grid.length==0 || grid[0].length==0){
            return 0;
        }
        
        this.grid = grid;
        this.row = grid.length;
        this.column = grid[0].length;
        this.count = row * column; 
        this.tempRegion = new int[row][column];

        for(int i = 0 ; i<row ; i++){
            for(int j = 0 ; j<column ; j++ ){
                if(grid[i][j]=='0'){ 
                    this.count--;
                }else{
                    tempRegion[i][j] = i * column + j;
                    if(i==0 && j==0){
                        continue;
                    }else if(i==0){
                        tempRegion[i][j] = j;
                        if(grid[i][j-1]!='0') union(j-1, j);
                    }else if(j==0){
                        tempRegion[i][j] = i*column;
                        if(grid[i-1][j]!='0') union((i-1)*column, i*column);
                    }else{
                        tempRegion[i][j] = i*column+j;
                        if(grid[i-1][j]!='0') union((i-1)*column+j, i*column+j);
                        if(grid[i][j-1]!='0') union(i*column+j-1, i*column+j);
                    }
                }
            }
        }
        return count;
    }
    
    private void union(int index1, int index2){
        int r1 = find(index1);
        int r2 = find(index2);
        if(r1==r2) return;
        tempRegion[r2/column][r2%column] = r1 ;
        count--;
    }
    
    private int find(int index){
        while(tempRegion[index/column][index%column] != index) index = tempRegion[index/column][index%column]; 
        return index;
    }
    

思路二:深度优先搜索

抛开从二者判断是否属于同一个并查集,我们从遍历的角度来看这个问题。其实如果我们没遇到一个陆地,就将属于该陆地的所有领域都标记为已经遍历过。那么下一次遇到一块新陆地的时候,该陆地一定是属于另一个版块。这种算法可以通过深度优先算法思想来实现。一旦遇到一块陆地,就递归的对上下左右的领域进行访问。该算法通过递归实现简洁高效!

    public int numIslands2(char[][] grid){
        if(grid==null || grid.length==0 || grid[0].length==0) return 0;
        this.row = grid.length;
        this.column = grid[0].length;
        int count = 0;
        for(int i = 0 ; i<row ; i++){
            for(int j = 0 ; j<column ; j++){
                if(grid[i][j]=='1'){
                    count++;
                    merge(grid, i, j);
                }
            }
        }
        return count;
    }
    
    public void merge(char[][] grid, int i , int j){
        if(i<0 || i>row || j<0 || j>column || grid[i][j] != '1') return;
        grid[i][j] = 'X';
        merge(grid, i-1, j);
        merge(grid, i+1, j);
        merge(grid, i, j-1);
        merge(grid, i, j+1);
    }

clipboard.png
想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~


raledong
2.7k 声望2k 粉丝

心怀远方,负重前行