LintCode: Number of Islands
分析:经典连通分量问题
图:
节点:所有1的位置
边:两个相邻的1的位置有一条边
BFS/DFS (DFS使用递归,代码较短)
选一个没标记的点,然后搜索,扩展4个邻居(如果有),直到不能扩展
每一次是一个连通分量
难点:标记节点——判重
C++
DFS
class Solution {
public:
void help(vector<vector<bool>>& a, int x, int y) {
if ((x < ) || (x >= a.size()) || (y < ) || (y >= a[x].size()) || a[x][y] == false) {
return ;
}
a[x][y] = false;
help(a, x + , y);
help(a, x - , y);
help(a, x, y + );
help(a, x, y - );
}
/**
* @param grid a boolean 2D matrix
* @return an integer
*/
int numIslands(vector<vector<bool>>& grid) {
// Write your code here
int ans = ;
for (int i = ; i < grid.size(); ++i) {
for (int j = ; j < grid[i].size(); ++j) {
if (grid[i][j] == true) {
help(grid, i, j);
++ans;
}
}
}
return ans;
}
};
C++
BFS
class Solution {
public:
void help(vector<vector<bool>>& a, int x, int y) {
queue<pair<int, int> > q;
const int dx[] = {-, , , };
const int dy[] = {, , -, };
a[x][y] = false;
for (q.push(make_pair(x, y)); !q.empty(); q.pop()) {
x = q.front().first;
y = q.front().second;
for (int i = ; i < ; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if ((nx >= ) && (nx < a.size()) && (ny >= ) &&(ny < a[nx].size()) && (a[nx][ny] == true)) {
a[nx][ny] = false;
q.push(make_pair(nx, ny));
}
}
}
}
/**
* @param grid a boolean 2D matrix
* @return an integer
*/
int numIslands(vector<vector<bool>>& grid) {
// Write your code here
int ans = ;
for (int i = ; i < grid.size(); ++i) {
for (int j = ; j < grid[i].size(); ++j) {
if (grid[i][j] == true) {
help(grid, i, j);
++ans;
}
}
}
return ans;
}
};
LintCode: Number of Islands的更多相关文章
- [LintCode] Number of Islands(岛屿个数)
描述 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], [0, 1, ...
- [LintCode] Number of Islands 岛屿的数量
Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...
- LintCode "Number of Islands II"
A typical Union-Find one. I'm using a kinda Union-Find solution here. Some boiler-plate code - yeah ...
- LintCode 433. 岛屿的个数(Number of Islands)
LintCode 433. 岛屿的个数(Number of Islands) 代码: class Solution: """ @param grid: a boolean ...
- 433. Number of Islands【LintCode java】
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...
- 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] Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LeetCode] 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 ...
随机推荐
- 将 tomcat 安装成 windows 服务
1.下载 tomcat 的windows 压缩包,一般以 .zip ,而且文件名中有 bin 的文件就是 2.解压下载的文件到某一个目录下,eg: TOMCAT_HOME 3.打开 cmd ,运行 % ...
- IOS开发之——objective-c与javascript交互
原文:http://blog.csdn.net/pjk1129/article/details/6936545 在写 JavaScript 的时候,可以使用一个叫做 window 的对象,像是我们想要 ...
- 用 setMessage 方法灵活复用 UIAlertView
- Android: INSTALL_FAILED_UPDATE_INCOMPATIBLE
from://http://xusaomaiss.iteye.com/blog/393296 在反复安装android apk的时候,有的时候可能会遇到adb install错误,内容是:Failur ...
- SpringBoot扫描包提示找不到mapper的问题
SpringBoot扫描包问题 报错信息:Consider defining a bean of type in your configuration 方法一: 使用注解 @ComponentScan ...
- 每天一个linux命令-lsof -i :port命令
使用lsof -i :port就能看见所指定端口运行的程序,同时还有当前连接. losf -i:port | wc -l,统计端口连接数
- Android之一种很有趣的界面跳动提示动画
上一个效果图: ==================================== 先上布局: <RelativeLayout xmlns:android="http://sch ...
- EditText 中文文档
本文来自:http://www.cnblogs.com/over140/archive/2010/09/02/1815439.html 属性名称 描述 android:autoLink 设置是否当文本 ...
- linux文件去重工具findup, fslint-gui
findup, fslint-gui,图形化工具,直接使用
- [转]wget 下载整个网站,或者特定目录
FROM : http://www.cnblogs.com/lidp/archive/2010/03/02/1696447.html 需要下载某个目录下面的所有文件.命令如下 wget -c -r - ...