分析:经典连通分量问题

图:

  节点:所有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的更多相关文章

  1. [LintCode] Number of Islands(岛屿个数)

    描述 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], [0, 1, ...

  2. [LintCode] Number of Islands 岛屿的数量

    Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...

  3. LintCode "Number of Islands II"

    A typical Union-Find one. I'm using a kinda Union-Find solution here. Some boiler-plate code - yeah ...

  4. LintCode 433. 岛屿的个数(Number of Islands)

    LintCode 433. 岛屿的个数(Number of Islands) 代码: class Solution: """ @param grid: a boolean ...

  5. 433. Number of Islands【LintCode java】

    Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...

  6. 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 用了第一种方式, ...

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

  8. [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 ...

  9. 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 ...

随机推荐

  1. 统计代码执行时间,使用Stopwatch和UserProcessorTime的区别

    当我们需要统计一段代码的执行时间,首先想到的可能是Stopwatch类.在这里,先暂不使用Stopwatch,自定义一个统计代码执行时间的类,大致需要考虑到: 1.确保统计的是当前进程.当前线程中代码 ...

  2. tms web core pwa让你的WEB APP离线可用

    tms web core pwa让你的WEB APP离线可用 tms web core允许创建渐进式Web应用程序(PWA).渐进式Web应用程序是为适应在线/离线情况,各种设备类型,最重要的是,让自 ...

  3. 【mysql】sum处理null的结果

    SELECT IFNULL() createSCNum, IFNULL() privateScNum FROM security_code_config WHERE tid = 'test_tenem ...

  4. Git:基础要点

    直接快照,而非比较差异. 近乎所有操作都可本地执行. 在Git 中的绝大多数操作都只需要访问本地文件和资源,不用连网.但如果用CVCS 的话,差不多所有操作都需要连接网络.因为Git 在本地磁盘上就保 ...

  5. 关于面试总结1-SQL学生表

    前言 每次面试必考SQL,小编这几年一直吃SQ的亏,考题无非就是万年不变学生表,看起来虽然简单,真正写出来,还是有一定难度.于是决定重新整理下关于SQL的面试题,也可以帮助更多的人过SQL这一关. 作 ...

  6. 找了一个api管理工具

    找了一个工具,https://github.com/nutsteam/apiManager选择了如下方式,进行了安装. ● 下载https://git.oschina.net/zhoujingjie/ ...

  7. 关于 java,nio,bufferedreader,bytebuffer

    有没有一种方法来读取的ByteBuffer有一个BufferedReader,而无需将其转换为String优先?我想读通过一个相当大的 ByteBuffer作为文本行和我想避免它写入磁盘性能方面的原因 ...

  8. BaseControl按钮合集

    BaseControl按钮合集 效果 源码 https://github.com/YouXianMing/Animations // // POPBaseControl.h // Animations ...

  9. Android Activity之间切换出现短暂黑屏的处理方法

    转自:http://www.cppblog.com/fwxjj/archive/2013/01/14/197259.html 在默认情况下,Android应用程序启动时,会有一个黑屏的时期,原因是,首 ...

  10. [A类会议] 国内论文检索

    https://www.cn-ki.net/ http://www.koovin.com