【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 ...
随机推荐
- 使用 CSS 接收用户的点击事情并对相关节点进行操作
问题背景:使用纯 CSS 方案,实现导航栏tab切换 实现 Tab 切换的难点在于如何使用 CSS 接收到用户的点击事情并对相关的节点进行操作.即是: 如何接收点击事件 如何操作相关DOM 下面看看如 ...
- java web文件下载功能实现 (转)
http://blog.csdn.net/longshengguoji/article/details/39433307 需求:实现一个具有文件下载功能的网页,主要下载压缩包和图片 两种实现方法: 一 ...
- 介绍一些有趣的MySQL pager命令
一.分页结果集 在Linux系统中中,我们经常也会使用一些分页查看命令,例如less.more等.同样,MySQL客户端也提供了类似的命令,用来帮助我们对查询结果集进行分页.比如,SHOW ENGIN ...
- Python基础案例教程
一.超市买薯片 # 用户输入薯片的单价 danjia = float(input("薯片的单价")) # 用户输入购买袋数 daishu = int(input("购买的 ...
- Mac关闭摄像头
为了防止黑客入侵,我们可以手动执行命令,关掉摄像头设备的访问权限,让黑客去看黑夜吧. Mac关闭禁用摄像头的方法,打开终端,拷贝如下代码,回车,输入管理员密码,再次拷贝,回车. sudo chmod ...
- Sublime Es6教程1-环境搭建
因为现在网上的教程都不靠谱,于是决定自己跳坑自己写,分为三块来玩: 一.环境搭建 二.语法讲解 三.项目实战 很多时候,你想搞一个东西,却因为环境没有搭建好,而不能很开森的探索未知的世界,多年的编程经 ...
- IDEA 不能显示项目里的文件结构
方法一: 关闭IDEA, 然后删除项目文件夹下的.idea文件夹 重新用IDEA工具打开项目 方法二: 菜单:File -> Invalidate Caches / Restart
- Intellij IDEA 配置Subversion插件时效解决方法
在使用Intellij的过程中,突然发现svn不起效了,在VCS–>Checkout from Version Control中也未发现Subversion这一项.如下图: 一.原因查找 经过分 ...
- Android 设计原则【转载+整理】
原文地址 本文内容 吸引我的眼球 简化我的生活 让我眼前一亮 在使用过大量 Android APP 后,你会发现,遵循了下面这些原则的 APP 将会有更好的用户体验. 我们知道,往往国企的那些软件,都 ...
- SuperMap iDesktop之导入数据
SuperMap作为一个平台软件有自己的数据格式,现要将ESRI的SHP数据导入到SuperMap的udb数据库中,可以完成导入,但也不得不说几点问题. 下面是ArcGIS中批量导入SHP的操作界面. ...