200. Number of Islands (Graph)
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(意思是,如果仅仅是斜角都为1,不是ajacent land). You may assume all four edges of the grid are all surrounded by water.
Example 1:
11110
11010
11000
00000
Answer: 1
Example 2:
11000
11000
00100
00011
Answer: 3
思路:找到没有标志的1,表示找到了一个island,然后找它的连通图,把这个连通图里的1都设置标志。
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int hight = grid.size();
if(hight == ) return ;
int width = grid[].size();
int ret = ; for(int i = ; i < hight; i++){
for(int j = ; j < width; j++){
if(grid[i][j]=='') continue; //find a land, continue to find land at its side
ret++;
grid[i][j] = ''; //set flag
dfs(grid, i+, j);
dfs(grid, i, j+);
dfs(grid, i-, j);
dfs(grid, i, j-);
}
}
return ret;
} void dfs(vector<vector<char>>& grid, int i, int j){
int hight = grid.size();
int width = grid[].size(); if(i>=hight || j>= width || i< || j<) return;
if(grid[i][j]=='') return; //find a land, continue to find land at its side
grid[i][j] = ''; //set flag
dfs(grid, i+, j);
dfs(grid, i, j+);
dfs(grid, i-, j);
dfs(grid, i, j-);
}
};
200. Number of Islands (Graph)的更多相关文章
- 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 用了第一种方式, ...
- 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 s ...
- 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 ...
- (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 ...
- 200. Number of Islands(DFS)
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 s ...
- [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 ...
随机推荐
- 登录之md5加密
语句: password = hex_md5(password); 引入js文件: md5.js: /* * A JavaScript implementation of the RSA Data S ...
- Nginx 服务器搭建
什么是Nginx ? Nginx与Apache IIS等软件一样,是一款服务器软件,为web站点提供服务 除此之外,Nginx 还是一款反向代理服务器,我们可以利用Nginx实现负载均衡 所谓负载均衡 ...
- python 3.4 error: Microsoft Visual C++ 10.0 is required(Unable to find vcvarsall.bat)
一些小技巧 我是在windows 64下安装的python3.4 Python 我在安装theano时报这个错,网上找了不少资料.自己摸索着解决了. 你先打开dos界面.我用set命令查看一下: 发现 ...
- sql server 查看所有表记录数
SELECT object_name (i.id) TableName, rows as rows FROM sysindexes i INNER JOIN sysObjects o ON (o.id ...
- Etcd源码解析(转)
7 Etcd服务端实现 7.1 Etcd启动 Etcd有多种启动方式,我们从最简单的方式入手,也就是从embed的etcd.go开始启动,最后会启动EtcdServer. 先看看etcd.go中的启动 ...
- git reset 版本回退
git log 查看所有提交信息. commit 67692318180bed6b2a17db0708cfbe0231e33db3 (HEAD -> master) Author: kingBo ...
- [ SHELL编程 ] 自动删除操作系统用户
Linux中经常需要删除用户,通常手工操作执行userdel操作即可,如果删除失败出现错误提示按照提示错误进行操作即可.如果是脚本需要调用删除用户操作呢?利用如下实例中drop_user删除用户函数, ...
- SSL协议(安全套接层协议)
SSL是Secure Sockets Layer(安全套接层协议)的缩写,可以在Internet上提供秘密性传输.Netscape公司在推出第一个Web浏览器的同时,提出了SSL协议标准.其目标是保证 ...
- openwrt多wan限上下行速脚本,基于qosv4,imq模块替换成ifb模块[ZT]
转自: http://www.right.com.cn/forum/thread-169414-1-1.html ,本人未经测试,转来自已备用 由于树莓派2装openwrt官方没有imq模块, 好像说 ...
- IPv4和IPv6的差异;如何实现IPv4和IPv6双协议栈的通信
1 IPv4和IPv6的差异 1.1 地址空间 IPv6 与 IPv4 比较最显著的一个改动就是使用 128 比特上的地址来代替了 32 比特长的 IPv4 地址. IPv6 中取消了广播地址, ...