[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.
Input:
11110
11010
11000
00000 Output: 1
Input:
11000
11000
00100
00011 Output: 3
思路:
- DFS and flip the bit-1 on the grid to 0 as we go: to 4 different directions
- Loop through all positions
- Visited spots won't be visited again because they are updated to '0'
怎样check 当前这格是否被visited过?
1.开一个boolean[][] sized as the original input matrix. 但需要额外空间
2.directly flag ‘2’ or ‘0’ (any char excpet ‘1’) in the original input matrix 插个小旗flag一下
相关小岛题变种
[leetcode]694. Number of Distinct Islands你究竟有几个异小岛?
代码:
class Solution {
public int numIslands(char[][] grid) {
if ( (grid.length==0) || (grid[0].length==0) ) return 0;
int count =0;
for (int i = 0; i<grid.length;i++) {
for (int j=0;j<grid[i].length;j++) {
if (grid[i][j]=='1') {
count++;
helper(grid, i, j);
}
}
}
return count;
}
private void helper(char[][] grid, int i, int j) {
if ((i<0) || (j<0) || (i>=grid.length) || (j>=grid[0].length)
|| grid[i][j] != '1' ) return;
grid[i][j] = '0'; // mark visited spot
helper(grid, i+1, j);
helper(grid, i-1, j);
helper(grid, i, j+1);
helper(grid, i, j-1);
}
}
[leetcode]200. Number of Islands岛屿个数的更多相关文章
- [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 ...
- 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 ...
- [leetcode]200. Number of Islands岛屿数量
dfs的第一题 被边界和0包围的1才是岛屿,问题就是分理出连续的1 思路是遍历数组数岛屿,dfs四个方向,遇到1后把周围连续的1置零,代表一个岛屿. /* 思路是:遍历二维数组,遇到1就把周围连续的1 ...
- 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 用了第一种方式, ...
- [LeetCode] 0200. Number of Islands 岛屿的个数
题目 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is su ...
- 【LeetCode】200. Number of Islands 岛屿数量
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- (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 ...
- Java for 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 ...
- 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 ...
随机推荐
- CentOS 7 快速部署 ELK
先简单说一下部署思路及原理(不一定正确,只是个人理解而已) 1.修改系统参数,以便安装软件 2.安装配置elasticsearch,这个相当于总监测中心,用来收集logstash监测的各种服务应用日志 ...
- JVM的基本结构及其各部分详解(二)
3.2 栈帧组成之操作数栈 操作数栈是栈帧的主要内容之一,它主要用于保存计算过程中的中间结果,同时作为计算过程中变量临时的存储空间. 操作数栈也是一个先进后出的数据结构,只支持入栈和出栈两种操作,许多 ...
- SVN远程管理
1.使用远程桌面连接工具,分别填写连接地址和用户名进行连接 2.紧接着进行安全登录. 3.打开VisualSVN Server Manager工具进行显示已经拥有的项目和分配好的群组管理. 这样子就可 ...
- docker登录运行中的容器的4方案
目前容器云非常的成熟,也有很多的使用案例,可以说不是什么高大上的东西了,可以说整个云计算也不是什么奢侈品,而是基础设施.但是如何使用,就成了必须的技术. 今天记录下,基于docker的容器登录技术. ...
- Excel技巧--做一去重复的数据下拉列表
当我们有一数据列表(内含重复数据),将该数据做成如下图的下拉列表: 可以这样做: 1.选中该标题行,按ctrl+shift+下方向键,将该列有数据的区域选中: 2.点击“数据”—>删除重复项: ...
- Hibernate复习
第一天 Hibernate是一个持久层的ORM框架.两个配置文件, 类名.hbm.xml类的属性和表的列对应 hibernate.cfg.xml核心配置文件 Hibernate相关API: Confi ...
- finstrument-functions
2017-12-03 23:59:16 参考 如何快速地在每个函数入口处加入相同的语句? https://www.zhihu.com/question/56132218 做个存档 scj@scjCom ...
- MII、GMII、RMII、SGMII、XGMII 接口区别
MII即媒体独立接口,也叫介质无关接口.它是IEEE-802.3定义的以太网行业标准.它包括一个数据接口,以及一个MAC和PHY之间的管理接口(图1). 数据接口包括分别用于发送器和接收器的两条独立信 ...
- sql server 索引碎片相关问题
1.查看表的索引碎片情况 --改成当前库 use DB_Name --创建变量 指定要查看的表 declare @table_id int set @table_id=object_id('Table ...
- mysql中间件kingshard
这样写是OK的: select * from bind_history limit 10;select id, passport_id, person_id, create_time, cast(is ...