【LeetCode】200. Number of Islands (2 solutions)
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:
11110
11010
11000
00000
Answer: 1
Example 2:
11000
11000
00100
00011
Answer: 3
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
对每次出现'1'的区域进行计数,同时深度或广度遍历,然后置为'0'。
解法一:非递归dfs
struct Node
{
int x;
int y;
Node(int newx, int newy): x(newx), y(newy) {}
}; class Solution {
public:
int numIslands(vector<vector<char>> &grid) {
int ret = ;
if(grid.empty() || grid[].empty())
return ret;
int m = grid.size();
int n = grid[].size();
for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(grid[i][j] == '')
{
dfs(grid, i, j, m, n);
ret ++;
}
}
}
return ret;
} void dfs(vector<vector<char>> &grid, int i, int j, int m, int n)
{
stack<Node*> stk;
Node* rootnode = new Node(i, j);
grid[i][j] = '';
stk.push(rootnode);
while(!stk.empty())
{
Node* top = stk.top();
if(top->x > && grid[top->x-][top->y] == '')
{//check up
grid[top->x-][top->y] = '';
Node* upnode = new Node(top->x-, top->y);
stk.push(upnode);
continue;
}
if(top->x < m- && grid[top->x+][top->y] == '')
{//check down
grid[top->x+][top->y] = '';
Node* downnode = new Node(top->x+, top->y);
stk.push(downnode);
continue;
}
if(top->y > && grid[top->x][top->y-] == '')
{//check left
grid[top->x][top->y-] = '';
Node* leftnode = new Node(top->x, top->y-);
stk.push(leftnode);
continue;
}
if(top->y < n- && grid[top->x][top->y+] == '')
{//check right
grid[top->x][top->y+] = '';
Node* rightnode = new Node(top->x, top->y+);
stk.push(rightnode);
continue;
}
stk.pop();
}
}
};

解法二:递归dfs
class Solution {
public:
int numIslands(vector<vector<char>> &grid) {
int ret = ;
if(grid.empty() || grid[].empty())
return ret;
int m = grid.size();
int n = grid[].size();
for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(grid[i][j] == '')
{
dfs(grid, i, j, m, n);
ret ++;
}
}
}
return ret;
}
void dfs(vector<vector<char>> &grid, int i, int j, int m, int n)
{
grid[i][j] = '';
if(i > && grid[i-][j] == '')
dfs(grid, i-, j, m, n);
if(i < m- && grid[i+][j] == '')
dfs(grid, i+, j, m, n);
if(j > && grid[i][j-] == '')
dfs(grid, i, j-, m, n);
if(j < n- && grid[i][j+] == '')
dfs(grid, i, j+, m, n);
}
};

【LeetCode】200. Number of Islands (2 solutions)的更多相关文章
- 【LeetCode】200. Number of Islands 岛屿数量
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【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
Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 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】1254. Number of Closed Islands
题目如下: Given a 2D grid consists of 0s (land) and 1s (water). An island is a maximal 4-directionally ...
随机推荐
- [Math]理解卡尔曼滤波器 (Understanding Kalman Filter)
1. 卡尔曼滤波器介绍 卡尔曼滤波器的介绍, 见 Wiki 这篇文章主要是翻译了 Understanding the Basis of the Kalman Filter Via a Simple a ...
- T 分布(近似标准正态分布)
1.1 定义 定义:假设X服从标准正态分布N(0,1),Y服从卡方分布,那么的分布称为自由度为n的t分布,记为. T分布密度函数其中,Gam(x)为伽马函数. 可用于两组独立计量资料的假设检 ...
- 【转】Linux基础与Linux下C语言编程基础
原文:https://www.cnblogs.com/huyufeng/p/4841232.html ------------------------------------------------- ...
- python绘制很美丽的图表
或许你会觉得python不适合做图形界面的开发,的确如此.可是python却有一个非常美丽的图标模块:pycha,废话少说,先上图,各位看一下. 是不是效果还不错呢,当然这仅仅是一小部分图表,还有其它 ...
- 使用Snap.svg类库实现的抖动式的幻灯播放效果
在线演示 本地下载 这个幻灯中,使用了SVG来生成具有动画弧度的幻灯背景效果,如果你在项目中能够支持现代浏览器的话,尝试一下这个效果吧,很赞! 想了解基础使用,观看这个轻视频吧:Snap.svg处理和 ...
- Android 高级 Jackson Marshalling(serialize)/Unmarshalling(deserialize)
本文内容 高级 Jackson Marshalling 只序列化符合自定义标准的字段 把 Enums 序列化成 JSON 对象 JsonMappingException(没有找到类的序列化器) Jac ...
- 从 bootup.js 学习加载脚本等资源
本文内容 如何使用 示例 参考资料 本文的目的在于,通过 bootup.js 的源代码,认识如何从客户端加载服务器的文件,特别是 JavaScript 文件,注入到页面,并存储在本地缓存,以扩展对 J ...
- slf4j、jcl、jul、log4j1、log4j2、logback大总结
1 系列目录 jdk-logging.log4j.logback日志介绍及原理 commons-logging与jdk-logging.log4j1.log4j2.logback的集成原理 slf4j ...
- Camtasia Studio CamStudio如何导出为手机视频
把视频拖放到左侧窗口,再按住拖放到下方的时间轴 点击生成并共享,然后设置为自定义生成设置 这里选择MP4,然后下一步 到这一步的时候,选择视频大小为自定义 会弹出窗口,手动输入宽360 ...
- PowerDesigner添加表注释
之前同事用PowerDesigner 建立数据模型后,生成到数据库中,没有注释.这导致数据库使用起来不是很方便,特别是对数据表结构不熟悉的同事. 其实,可以添加注释(并且可以逆向,即从数据库中反向更新 ...