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.

解题思路:

参考Java for LeetCode 130 Surrounded Regions BFS即可

JAVA实现如下:

    public int numIslands(char[][] grid) {
int res = 0;
for (int i = 0; i < grid.length; i++)
for (int j = 0; j < grid[0].length; j++)
if (grid[i][j] == '1') {
grid[i][j] = '0';
bfs(grid, i * grid[0].length + j);
res++;
}
return res;
} public static void bfs(char[][] grid, int num) {
Queue<Integer> queue = new LinkedList<Integer>();
queue.add(num);
while (!queue.isEmpty()) {
num = queue.poll();
int row = num / grid[0].length;
int col = num - row * grid[0].length;
if (row - 1 >= 0 && grid[row - 1][col] == '1') {
grid[row - 1][col] = '0';
queue.add(num - grid[0].length);
}
if (row + 1 <= grid.length - 1 && grid[row + 1][col] == '1') {
grid[row + 1][col] = '0';
queue.add(num + grid[0].length);
}
if (col - 1 >= 0 && grid[row][col - 1] == '1') {
grid[row][col - 1] = '0';
queue.add(num - 1);
}
if (col + 1 <= grid[0].length - 1 && grid[row][col + 1] == '1') {
grid[row][col + 1] = '0';
queue.add(num + 1);
}
}
}

Java for LeetCode 200 Number of Islands的更多相关文章

  1. [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 surro ...

  2. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  3. LeetCode 200. Number of Islands 岛屿数量(C++/Java)

    题目: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is s ...

  4. [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 surro ...

  5. 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 surro ...

  6. [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 surro ...

  7. (BFS/DFS) 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 surro ...

  8. Leetcode 200 Number of Islands DFS

    统计联通区域块的个数,简单dfs,请可以参考DFS框架:Leetcode 130 Surrounded Regions DFS class Solution { public: int m, n; b ...

  9. [leetcode]200. Number of Islands岛屿数量

    dfs的第一题 被边界和0包围的1才是岛屿,问题就是分理出连续的1 思路是遍历数组数岛屿,dfs四个方向,遇到1后把周围连续的1置零,代表一个岛屿. /* 思路是:遍历二维数组,遇到1就把周围连续的1 ...

随机推荐

  1. 38.Android之ListView简单学习(一)

    android中ListView用的很普遍,今天来学习下,本篇主要以本地数据加载到listview,后面会学习从网络获取数据添加到listview. 首先改下布局文件: <?xml versio ...

  2. TYVJ1864 守卫者的挑战

    P1864 [Poetize I]守卫者的挑战 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜 ...

  3. Chkrootkit Sourcecode Learning

    目录 . Chkrootkit Introduce . Source Code Frame . chklastlog.c . chkwtmp.c . ifpromisc.c . chkproc.c . ...

  4. python二维数组

    和c c++不一样 过程如下: #-*- coding:utf-8 -*- t = [[ 0 for i in range(5)]for j in range(5)] for i in range(5 ...

  5. c++中string类型用下标初始化后str.size()为0 输出string值为空

    你的string list是个默认构造函数,这样就没有为list分配空间,自然list[i]就会报出超出string范围的错误,可以简单更改为string list(6, '\0'),事先为list指 ...

  6. 彻底理解position与anchorPoint

    引言 相信初接触到CALayer的人都会遇到以下几个问题: 为什么修改anchorPoint会移动layer的位置?CALayer的position点是哪一点呢?anchorPoint与positio ...

  7. spark.SecurityManager: SecurityManager: authentication disabled

  8. SQLServer 数据导入导出 SSIS 包 位置

    笔记:sqlserver 在执行数据导入导出的时候,可以选择是否保存SSIS包,如果选择保存,在保存方式有:SQlserver .文件系统.如果选择sqlserver 则 包信息保存在 msdb 系统 ...

  9. derby数据库ql语法

    [数据库知识] 主键.唯一键包含索引 主键包含唯一键.索引.非空 唯一键包含索引,可空或非空 数据库需要与执行服务的在同个目录下 唯一键 create table app.tyu ( primaryk ...

  10. http 超文本传输协议

    超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准.设计HTTP最初的目的是为了提供一种发布和接 ...