第八篇 LeetCode Number of Islands

好久没更新了,终于考完了继续回来刷题

岛屿问题属于最基本的DFS, BFS题目 使用DFS时会遇到如果图太大call stack过深的follow up,此时可以转为使用BFS

下一篇Number of Islands II 中将使用另一种Union Find的做法

public class Solution {
    public int numIslands(char[][] grid) {
        if (grid == null || grid.length == 0) {
            return 0;
        }
        int result = 0;
        int m = grid.length;
        int n = grid[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                //if gird[i][j] is not visited
                if (grid[i][j] == ‘1‘) {
                    bfs(grid, i, j);
                    result++;
                }
            }
        }
        return result;
    }

    private void bfs(char[][] grid, int a, int b) {
        int[] x = {1, 0, -1, 0};
        int[] y = {0, -1, 0, 1};
        grid[a][b] = ‘0‘;
        Queue<Pair> q = new LinkedList<Pair>();
        q.offer(new Pair(a, b));
        while (!q.isEmpty()) {
            Pair p = q.poll();
            for (int i = 0; i < 4; i++) {
                int x1 = p.x + y[i];
                int x2 = p.y + x[i];
                if (x2 >= 0 && x2 < grid[0].length && x1 >= 0 && x1 < grid.length) {
                    if (grid[x1][x2] == ‘1‘) {
                        grid[x1][x2] = ‘0‘;
                        q.offer(new Pair(x1, x2));
                    }
                }
            }
        }

    }

    private class Pair {
        int x;
        int y;

        public Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

    private void dfs(char[][] grid, int a, int b) {
        grid[a][b] = ‘0‘;
        int[] x = {1, 0, -1, 0};
        int[] y = {0, -1, 0, 1};

        for (int i = 0; i < 4; i++) {
            int x1 = a + y[i];
            int x2 = b + x[i];
            if (x2 >= 0 && x2 < grid[0].length && x1 >= 0 && x1 < grid.length) {
                if (grid[x1][x2] == ‘1‘) {
                    //grid[x1][x2] = ‘0‘;
                    dfs(grid, x1, x2);
                }
            }
        }
    }
}
时间: 2024-10-25 21:12:58

第八篇 LeetCode Number of Islands的相关文章

第九篇 LeetCode Number of Islands II

UnifonFind with path compression and weighting O(klogmn), k is the length of positions array public class Solution { public List<Integer> numIslands2(int m, int n, int[][] positions) { List<Integer> result = new ArrayList<Integer>(); if

[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] 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

[LeetCode] Number of Islands II

Problem Description: 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

LeetCode Number of Islands 岛的数量(DFS,BFS)

题意:0代表水,1代表陆地,那么被水围起来的就是岛了,给一个01矩阵,问有多少个岛? 思路:DFS还是比较短,实现了一下.如果一个点已经被遍历过了,那就将其置为0就行了,不要去搜0的. 1 class Solution { 2 public: 3 bool isok(vector<vector<char> >& grid,int x,int y) 4 { 5 return x>=0 && y>=0 && x<grid.siz

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] 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