【Java】【LeetCode】200. Number of Islands

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:

Input:
11110
11010
11000
00000

Output: 1

Example 2:

Input:
11000
11000
00100
00011

Output: 3
import java.util.LinkedList;
import java.util.Queue;

public class NumberofIslands
{

    public static void main(String[] args)
    {
        /**
         * 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:
         * 
         * Input:
         * 11110
         * 11010
         * 11000
         * 00000
         * 
         * Output: 1
         * Example 2:
         * 
         * Input:
         * 11000
         * 11000
         * 00100
         * 00011
         * 
         * Output: 3
         */
        char[][] grid1 = {
            { '1', '1', '1', '1', '0' },
            { '1', '1', '0', '1', '0' },
            { '1', '1', '0', '0', '0' },
            { '0', '0', '0', '0', '0' } };
        System.out.println(numIslands(grid1));
        char[][] grid2 = {
            { '1', '1', '0', '0', '0' },
            { '1', '1', '0', '0', '0' },
            { '0', '0', '1', '0', '0' },
            { '0', '0', '0', '1', '1' } };
        System.out.println(numIslands(grid2));
        char[][] grid3 = {
            { '1', '1', '1', '1', '1', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '0', '1', '0', '1', '1' },
            { '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '0', '1', '1', '1', '1', '1', '0' },
            { '1', '0', '1', '1', '1', '0', '0', '1', '1', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1' },
            { '1', '1', '1', '1', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1' },
            { '1', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1' },
            { '1', '0', '1', '1', '1', '1', '1', '1', '0', '1', '1', '1', '0', '1', '1', '1', '0', '1', '1', '1' },
            { '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '0', '1', '1', '0', '1', '1', '1', '1' },
            { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '0', '1', '1', '1', '1', '0', '1', '1' },
            { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1' },
            { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1' },
            { '0', '1', '1', '1', '1', '1', '1', '1', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1' },
            { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1' },
            { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1' },
            { '1', '1', '1', '1', '1', '0', '1', '1', '1', '1', '1', '1', '1', '0', '1', '1', '1', '1', '1', '1' },
            { '1', '0', '1', '1', '1', '1', '1', '0', '1', '1', '1', '0', '1', '1', '1', '1', '0', '1', '1', '1' },
            { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '0', '1', '1', '1', '1', '1', '1', '0' },
            { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '0', '1', '1', '1', '1', '0', '0' },
            { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1' },
            { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1' },
            { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1' } };
        System.out.println(numIslands(grid3)); // Output: 1
    }

    public static int numIslands(char[][] grid)
    {
        if (grid == null || grid.length == 0 || grid[0] == null || grid[0].length == 0)
        {
            return 0;
        }
        int count = 0, col = grid[0].length, row = grid.length;
        Queue<int[]> queue = new LinkedList<int[]>(); // 記錄已經被遍歷的區域的點索引
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                if (grid[i][j] == '1') // 找到某塊爲1的區域,遍歷過後改爲'2'
                {
                    count++; // 每次遇到新的區域便加1
                    queue.offer(new int[] { i, j });
                    while (!queue.isEmpty())
                    {
                        int[] tmp = queue.poll();
                        int m = tmp[0], n = tmp[1];
                        if (grid[m][n] == '1')
                        {
                            grid[m][n] = '2';
                            if (m - 1 >= 0 && grid[m - 1][n] == '1')
                            {
                                queue.offer(new int[] { m - 1, n });
                            }
                            if (m + 1 <= row - 1 && grid[m + 1][n] == '1')
                            {
                                queue.offer(new int[] { m + 1, n });
                            }
                            if (n - 1 >= 0 && grid[m][n - 1] == '1')
                            {
                                queue.offer(new int[] { m, n - 1 });
                            }
                            if (n + 1 <= col - 1 && grid[m][n + 1] == '1')
                            {
                                queue.offer(new int[] { m, n + 1 });
                            }
                        }
                    }
                }
            }
        }
        return count;
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章