Number of Islands——LeetCode

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:

11110110101100000000

Answer: 1

Example 2:

11000110000010000011

Answer: 3

哈哈,这是今天的新题,十多分钟A了,题目不难,我的做法是BFS+DFS,题目大意是1代表岛屿,0代表水,可以假设这个grid周围都是水,岛屿定义是四周都是水,求岛屿的个数。

我的做法是,从第一个开始,如果是1,并且没有访问过visit为false,那么加入queue里,当queue非空取出来并加入它上下左右的邻居,并且把这些节点visit设置为true,时间复杂度是O(M*N),空间复杂度的话,因为同时在queue里的最多也就4个节点的坐标,所以是O(1)。

Talk is cheap>>

public int numIslands(char[][] grid) {
        if (grid == null || grid.length == 0) {
            return 0;
        }
        int rowLen = grid.length;
        int colLen = grid[0].length;
        boolean[][] visited = new boolean[rowLen][colLen];
        int[][] direct = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        Deque<Integer> queue = new ArrayDeque<>();
        int res = 0;
        for (int i = 0; i < rowLen; i++) {
            for (int j = 0; j < colLen; j++) {
                if (!visited[i][j] && grid[i][j] == ‘1‘) {
                    res++;
                    queue.add(i * colLen + j);
                    while (!queue.isEmpty()) {
                        int pos = queue.poll();
                        int curr_x = pos / colLen;
                        int curr_y = pos % colLen;
                        if (visited[curr_x][curr_y]) {
                            continue;
                        }
                        visited[curr_x][curr_y] = true;
                        for (int k = 0; k < 4; k++) {
                            int x = curr_x + direct[k][0];
                            int y = curr_y + direct[k][1];
                            if (x >= 0 && y >= 0 && x < rowLen && y < colLen && grid[x][y] == ‘1‘) {
                                queue.add(x * colLen + y);
                            }
                        }
                    }
                }
            }
        }
        return res;
    }
时间: 2024-12-26 01:03:22

Number of Islands——LeetCode的相关文章

Number of Islands -- leetcode

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

[LeetCode][JavaScript]Number of Islands

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

&lt;LeetCode OJ&gt; 200. Number of Islands

Total Accepted: 48411 Total Submissions: 171609 Difficulty: Medium 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 verticall

[LeetCode] Number of Islands II 岛屿的数量之二

A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each addLand o

[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

LeetCode | 0200. Number of Islands岛屿数量【Python】

LeetCode 0200. Number of Islands岛屿数量[Medium][Python][DFS] Problem LeetCode 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 v

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

[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

【LeetCode】200. Number of Islands (2 solutions)

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