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)的更多相关文章

  1. 【LeetCode】200. Number of Islands 岛屿数量

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  2. 【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 ...

  3. 【刷题-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. ...

  4. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  5. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  6. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  7. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  8. 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 ...

  9. 【leetcode】1254. Number of Closed Islands

    题目如下: Given a 2D grid consists of 0s (land) and 1s (water).  An island is a maximal 4-directionally ...

随机推荐

  1. scikit-learn——快速入门 - daniel-D(转)

    ML sklearn快速入门 申明:该系列博客是学习 sklearn 的笔记,内容将涵盖大部分机器学习的方法.本人微博@迅猛龙Daniel,能力有限,存在任何问题,希望共同交流.该博客采用马克飞象专业 ...

  2. Concurrent HTTP connections in Node.js

    原文: https://fullstack-developer.academy/concurrent-http-connections-in-node-js/ -------------------- ...

  3. Vs2013 坑爹的Target framework问题

    之前的一个项目是使用Vs2008来开发的,因为这段时间家里有事情所以只能跟经理协商在家里来做此项目,因为家里的VS是2013的所以在迁移时没有什么问题 但今天我更改一个类库的文件后重新生成解决方案结果 ...

  4. [Docker] Building a Node.js Image

    Create a Dockerfile: FROM node:latest MAINTAINER Zhentian Wan ENV NODE_ENV=production ENV PORT= COPY ...

  5. 【Android开发VR实战】二.播放360&#176;全景视频

    转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/53924006 本文出自[DylanAndroid的博客] [Android开发 ...

  6. UltraISO制作U盘启动盘安装Win7/9/10系统攻略

    U盘安装好处就是不用使用笨拙的光盘,光盘还容易出现问题,无法读取的问题.U盘体积小,携带方便,随时都可以制作系统启动盘. U盘建议选择8G及以上大小的. 下面一步一步讲解如果制作U盘安装盘: 1.首先 ...

  7. IntelliJ - idea15.0.2 破解方法

    由于idea 15版本更换了注册方式,只能通过联网激活,所以现在不能通过简单的通用注册码进行离线注册了, 虽然可以继续用14版本,但是有新版本却无法尝试让强迫症也是异常抓狂. 通过度娘我找到了一个破解 ...

  8. Docker 容器入门

    1.1 容器简介 1.1.1 什么是 Linux 容器 Linux容器是与系统其他部分隔离开的一系列进程,从另一个镜像运行,并由该镜像提供支持进程所需的全部文件.容器提供的镜像包含了应用的所有依赖项, ...

  9. C#.NET常见问题(FAQ)-如何判断某个字符是否为汉字

    字符强制转换成int可以判断字符数值大小,在下面所示范围内的就是中文   此外还可以判断是否是数字或者字母,用char.IsLetter和char.IsDigit方法   从先这个范例可以看出,中文也 ...

  10. ArcMap工具箱参数名称的Bug

    已经忍了很久了,今天一定要说一说,强大的 ArcGIS居然还存在这种Bug问题.如下图所示:使用了追加工具,有三个要素图层,且三个数据与目标数据不是同一数据,但它们的名称一致,这样执行,将会出现&qu ...