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:

  1. 11110
    11010
    11000
    00000

Answer: 1

Example 2:

  1. 11000
    11000
    00100
    00011

Answer: 3

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

对每次出现'1'的区域进行计数,同时深度或广度遍历,然后置为'0'。

解法一:非递归dfs

  1. struct Node
  2. {
  3. int x;
  4. int y;
  5. Node(int newx, int newy): x(newx), y(newy) {}
  6. };
  7.  
  8. class Solution {
  9. public:
  10. int numIslands(vector<vector<char>> &grid) {
  11. int ret = ;
  12. if(grid.empty() || grid[].empty())
  13. return ret;
  14. int m = grid.size();
  15. int n = grid[].size();
  16. for(int i = ; i < m; i ++)
  17. {
  18. for(int j = ; j < n; j ++)
  19. {
  20. if(grid[i][j] == '')
  21. {
  22. dfs(grid, i, j, m, n);
  23. ret ++;
  24. }
  25. }
  26. }
  27. return ret;
  28. }
  29.  
  30. void dfs(vector<vector<char>> &grid, int i, int j, int m, int n)
  31. {
  32. stack<Node*> stk;
  33. Node* rootnode = new Node(i, j);
  34. grid[i][j] = '';
  35. stk.push(rootnode);
  36. while(!stk.empty())
  37. {
  38. Node* top = stk.top();
  39. if(top->x > && grid[top->x-][top->y] == '')
  40. {//check up
  41. grid[top->x-][top->y] = '';
  42. Node* upnode = new Node(top->x-, top->y);
  43. stk.push(upnode);
  44. continue;
  45. }
  46. if(top->x < m- && grid[top->x+][top->y] == '')
  47. {//check down
  48. grid[top->x+][top->y] = '';
  49. Node* downnode = new Node(top->x+, top->y);
  50. stk.push(downnode);
  51. continue;
  52. }
  53. if(top->y > && grid[top->x][top->y-] == '')
  54. {//check left
  55. grid[top->x][top->y-] = '';
  56. Node* leftnode = new Node(top->x, top->y-);
  57. stk.push(leftnode);
  58. continue;
  59. }
  60. if(top->y < n- && grid[top->x][top->y+] == '')
  61. {//check right
  62. grid[top->x][top->y+] = '';
  63. Node* rightnode = new Node(top->x, top->y+);
  64. stk.push(rightnode);
  65. continue;
  66. }
  67. stk.pop();
  68. }
  69. }
  70. };

解法二:递归dfs

  1. class Solution {
  2. public:
  3. int numIslands(vector<vector<char>> &grid) {
  4. int ret = ;
  5. if(grid.empty() || grid[].empty())
  6. return ret;
  7. int m = grid.size();
  8. int n = grid[].size();
  9. for(int i = ; i < m; i ++)
  10. {
  11. for(int j = ; j < n; j ++)
  12. {
  13. if(grid[i][j] == '')
  14. {
  15. dfs(grid, i, j, m, n);
  16. ret ++;
  17. }
  18. }
  19. }
  20. return ret;
  21. }
  22.  
  23. void dfs(vector<vector<char>> &grid, int i, int j, int m, int n)
  24. {
  25. grid[i][j] = '';
  26. if(i > && grid[i-][j] == '')
  27. dfs(grid, i-, j, m, n);
  28. if(i < m- && grid[i+][j] == '')
  29. dfs(grid, i+, j, m, n);
  30. if(j > && grid[i][j-] == '')
  31. dfs(grid, i, j-, m, n);
  32. if(j < n- && grid[i][j+] == '')
  33. dfs(grid, i, j+, m, n);
  34. }
  35. };

【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. 详细解读简单的lstm的实例

    http://blog.csdn.net/zjm750617105/article/details/51321889 本文是初学keras这两天来,自己仿照addition_rnn.py,写的一个实例 ...

  2. nginx不浏览直接下载文件

    当我们使用Nginx时,如果要让一些附件比如txt,pdf,doc等不直接在浏览器打开,而弹出另存为的对话框(也就是下载),则可以在nginx里添加如下配置: location /{if ($requ ...

  3. Java归去来第1集:手动给Eclipse配置Maven环境

    一.Eclipse配置Maven 1.1.下载Maven http://maven.apache.org/download.cgi,选择对应的版本,window下载apache-maven-3.5.3 ...

  4. ecshop二次开发 使用ecshop电子商务系统的100个小问题

    自己从事B4C电子商务开发一段时间了,特别对ecshop深有体会,刚接触的时候不容易理解,下面将根据自己的经验,来总结100条关于操作ecshop电子商务系统的小问题. 1:如何修改网站"欢 ...

  5. .Net C# 5.0 规范:迭代器

    本文内容 枚举器 enumerator 接口 - IEnumerator 可枚举 enumerable 接口 - IEnumerable 产生类型 yield type 枚举器 enumerator ...

  6. 【Ubuntu】Ubuntu bug “Reached target Shutdown”

    在使用 Ubuntu-16.04.3-server-amd64.iso 版本(参考官方,iso下载地址)的时候,遇到一个Ubuntu的Bug,后来查了一下,在官方看到,目前还是Unassigned: ...

  7. Direct hosting of SMB over TCP/IP

    http://support.microsoft.com/kb/204279 System TipThis article applies to a different version of Wind ...

  8. Netcore使用MailKit进行邮件发送

    public void TestSendMailDemo() { var message = new MimeKit.MimeMessage(); message.From.Add(new MimeK ...

  9. 深度学习-Caffe编译测试的小总结

    1. 搭建的环境和代码:win7 64bit + vs2013+CUDA7.5 http://blog.csdn.net/thesby/article/details/50880802 2. 编译,制 ...

  10. 将Spring-boot应用部署到Docker容器

    1:Docker中设置阿里云加速 使用阿里云的加速器,因为在使用docker的时候,会需要从docker的网站下载镜像文件,下载速度可能会很慢.获得阿里云加速,需要登录阿里云开发者平台,然后点击右侧的 ...