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. ES6 你可能不知道的事 – 基础篇

    序 ES6,或许应该叫 ES2015(2015 年 6 月正式发布),对于大多数前端同学都不陌生. 首先这篇文章不是工具书,不会去过多谈概念,而是想聊聊关于每个特性 你可能不知道的事,希望能为各位同学 ...

  2. 【Gym 100712A】Who Is The Winner?

    题 题意 解题数目越多越排前,解题数目相同罚时越少越排前,求排第一的队伍名字. 分析 用结构体排序. 代码 #include<cstdio> #include<algorithm&g ...

  3. h5页面,改变数字默认颜色

    最近遇到一个非常变态的bug,有一串数字,我设置color为白色,在pc端浏览器,无变化,但是到了手机端,会由白色跳成黑色,我无解啊... 刚刚找到方法,如下: <meta name=" ...

  4. UVA 11527 Unique Snowflakes

    用STL做会很方便 SET: /*by SilverN*/ #include<iostream> #include<algorithm> #include<cstring ...

  5. 在纯HTML的静态网页中添加一段统计网页访问量的JAVA Script代码?

    如何在网站上进行流量统计呢,可以找第三方服务网站去注册,但也可以在网站上直接添加代码,只需将以下代码copy到你的网页中,复制到</body>之前就可以啦!是不是很简单啊! <scr ...

  6. form的submit与onsubmit的用法与区别

    发生顺序:onsubmit -> submit1.阻止表单提单:<script>function submitFun(){    //逻辑判断    return true; //允 ...

  7. IOS基础之 (三) 类的声明和对象的创建

    一 OC类的声明和实现语法 1.接口的声明 @interface NewClassName: ParentClassName { 实例变量 ... } 方法的声明 ... @end //...表示省略 ...

  8. android加载大图片到内存

    1)演示效果: 1)代码演示: 布局代码: 权限配置:

  9. awk命令--update20150120

    简介 awk是一个强大的文本分析工具,把文件逐行读入,以空格为默认分隔符分割成field,切开的部分再进行各种分析处理. 模式和动作: 任何awk语句都是由模式和动作组成,模式部分决定动作语句何时触发 ...

  10. WIN 2003服务器终极安全及问题解决方案

    一.硬盘分区与操 作系统的安装硬盘分区 总的来讲在硬盘分区上面没什么值得深入剖析的地方,无非就是一个在分区前做好规划知道要去放些什么东西, 如果实在不知 道.那就只一个硬盘只分一个区,分区要一次性完成 ...