LC 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.
Example 1:
Input:
11110
11010
11000
00000 Output: 1
Example 2:
Input:
11000
11000
00100
00011 Output: 3
参考答案
class Solution {
public:
void foo(vector<vector <char>> &grid,int i, int j){
if(i< || j< || i>=grid.size() || j>=grid[].size()) return ;
if(grid[i][j] == '' ) return; //为了递归准备的停止条件
grid[i][j] = ''; // 为了避免重复,直接将检测过的value归零
foo(grid,i-,j);
foo(grid,i+,j);
foo(grid,i,j-);
foo(grid,i,j+); } int numIslands(vector<vector<char>>& grid) {
int counter = ;
for(int i = ; i<grid.size();i++){
for(int j = ; j<grid[i].size();j++){
if(grid[i][j] == ''){
counter ++;
foo(grid,i,j);
}
}
}
return counter;
}
};
额外补充
SRGA
这道题目让我想到了 种子区域生长算法(Seeded Regin Growing Algorithm),参考了一下别人的答案,还真的十分类似。
基本思路是:种下一个种子(遇到的第一个1区域),然后让它生长(递归)。如果是相同区域,就生长(7~10 行),如果不同(4~5行),就停止。
因为区域种子生长是为了,给所有邻接的区域标记label,从而更好识别图象。但这里不需要label,只需要counter。
防止重复
由初始点位,开始生长,由于递归规则相同,所以初始点位也会进入递归,为了防止重复,只要被接触的区域,全部重新赋值为0。(6行)
的确是一个很聪明的做法。
LC 200 Number of Islands的更多相关文章
- 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 ...
随机推荐
- JavaWeb_(Mybatis框架)使用Mybatis对表进行增、删、改、查操作_二
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- 1 - ImageNet Classification with Deep Convolutional Neural Network (阅读翻译)
ImageNet Classification with Deep Convolutional Neural Network 利用深度卷积神经网络进行ImageNet分类 Abstract We tr ...
- myeclipse使用SVN分支与合并详解
此博文主要内容来源地址:https://blog.csdn.net/liuyifeng1920/article/details/53118183,感谢原创博主: 先介绍一下svn的两种开发和发布的规范 ...
- 2018-2019-2 20165330《网络对抗技术》Exp7 网络欺诈防范
目录 基础问题 相关知识 实验目的 实验内容 实验步骤 实验中遇到的问题 实验总结与体会 实验目的 本实践的目标理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法. 返回目录 实验内容 简 ...
- C# Read/Write another Process' Memory
https://codingvision.net/security/c-read-write-another-process-memory Today’s tutorial is about…proc ...
- 使用 Jenkins 发布 web 应用至 Tomcat
Jenkins 的安装:https://www.cnblogs.com/jhxxb/p/11406805.html 发布流程:Git -> Maven -> Tomcat 一.基本环境 G ...
- 1753 -- Flip Game
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48663 Accepted: 20724 Descr ...
- LaTeX统计文章字数
今天学会了用LaTeX命令统计文章字数,命令如下: textcount doc_full_path.tex 效果如下:
- python从入门到放弃之Tensorflow(一)
Tensorflow使用错误集锦: 错误1 : FutureWarning: Conversion of the second argument of issubdtype from ‘float’ ...
- Docker监控:最佳实践以及cAdvisor和Prometheus监控工具的对比
在DockerCon EU 2015上,Brian Christner阐述了“Docker监控”的概况,分享了这方面的最佳实践和Docker stats API的指南,并对比了三个流行的监控方案:cA ...