【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 ...
随机推荐
- JPA(三):JPA基本注解
基本注解 @Entity 标注用于实体类声明语句之前,指出该Java类为实体类,将映射到指定的数据库表.如声明一个实体类Customer,将它映射到数据的coustomer表上. package co ...
- Linux echo 显示内容颜色
Linux echo 显示内容颜色 https://www.cnblogs.com/kimbo/p/6816566.html #字体颜色:30m-37m 黑.红.绿.黄.蓝.紫.青.白 str=&qu ...
- Cognos11中通过URL访问report的设置
1:以往的cognos版本中在报表的属性中可以找到 url的属性,稍加修改就可以通过URL进行访问了 2:Cognos11中找了半天也没有报表URL这个属性,但是IBM官方也给出了解决方案 Answe ...
- (转)AssetBundle系列——共享资源打包/依赖资源打包
有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数: BuildPipeline.PushAssetDependencies():依赖资源压栈: BuildPipeline.P ...
- MongoDB的Invalid credentials for database
前面都好好的,结果服务器数据库加了一个验证,查了一下,也不算复杂,只要把连接串一改就行了. 结果,不断报错——Invalid credentials for database 找了半天原因,原来是我用 ...
- Get file extention in XSLT
When working with data view web parts or data form web parts in SharePoint, you might want to use ...
- Android Jackson 概述
原文地址 本文内容 JSON 的三种方式 示例 完全数据绑定(POJO)示例 "Raw"数据绑定示例 用泛型数据绑定 树模型(Tree Model)示例 流(Streaming)A ...
- MogileFS与FastDFS的个人见解
MogileFS与FastDFS的个人见解 六月 9, 2013 1 条评论 MogileFS & FastDFS 为两个开源分布式文件系统,都主要适用于互联网文件共享,上传,下载等功能,主要 ...
- 从C++到java
C++和java都号称是面向对象的语言,虽然C++不完全算是.学习过C++如何快速对java有个大体的掌握,可以通过对比来进行了解. 首先还是来高大上一下,看看他们的使命: · C++ 被设计成主要用 ...
- vsphere 5.1 性能最佳实践。
1.关于CPU负载.extop显示的结果 如果CPU load average>=1,说明主机过载了. 如果PCPU used%在80%左右说明良好,90%以上就临近过载了. VM赋予过多的vC ...