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 ...
随机推荐
- 虚拟视点demo
2019年7月16日15:55:11 感觉虚拟视点也是视觉slam里头一个重要的需求和应该实现的功能,但是好像 没看到什么资料. 百度的全景地图,或者有些公司网站上的3d装修效果图,可以用鼠标拖动查看 ...
- Love to be loved by you & Just one last dance
http://baike.baidu.com/link?url=wOnBuPncIH5b5oWc0ZREXCU8x6XPYqlZazTLarTjE8eOpdtpv57YMeB_kgXQq4BcCeh2 ...
- 用python实现的简易记牌器的demo
实现功能很简单: 初始时 1到10 以及 jkq各 4张,大小王 共两张 只要输入相应的牌号:1到10,例如 >>1 J.K.Q :例如>>j >> ...
- LightGBM新特性总结
LightGBM提出两种新方法:Gradient-based One-Side Sampling (GOSS) 和Exclusive Feature Bundling (EFB)(基于梯度的one-s ...
- 数据库 | SQL查询&LIMIT的用法
body{ text-align:left; width:80%; margin:10px 100px; } 前言 select top n 形式的语句可以获取查询的前几个记录,但是 mysql没有此 ...
- P1115 最大子段和&P1719 最大加权矩形
上接:DP&图论 DAY 1 上午 这两个题本质是一个亚子,所以放一起啦 DPDPDPDPDPDPDPDP P1115 最大子段和 题解 因为题目要求的是一段连续的区间,所以前缀和搞暴力??? ...
- 前端知识点回顾之重点篇——ES6的async函数和module
async函数 ES2017 标准引入了 async 函数,使得异步操作变得更加方便. async 函数是 Generator 函数的语法糖 什么是语法糖? 意指那些没有给计算机语言添加新功能,而只是 ...
- 在AndroidStudio中使用单元测试
1. 前言 在Android开发中,如果对一个简单的功能,每次修改代码都重新运行到设备中进行测试,会浪费大量时间,降低开发工作效率.如果使用单元测试,编写单元测试类,执行测试单元测试类就可以对 ...
- linux centos6.5 环境下安装redis的过程
过程还是挺折磨人的!谢谢许正同学一直耐心给我指导,虽然他也很忙.废话不多说: 首先,确保linux虚拟机联网: vm虚拟机>设置>Network Adapter 设置>网络配置设置成 ...
- Hadoop、spark
http://blog.csdn.net/u011204847/article/details/51355272