zoukankan      html  css  js  c++  java
  • 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

    Analyse: BFS or DFS. If found a '1', label that element as visited, and find all elements could be reached from that element and label them. 

    Runtime: Iteration 12ms

     1 class Solution {
     2 public:
     3     int numIslands(vector<vector<char>>& grid) {
     4         if(grid.empty() || grid[0].empty()) return 0;
     5         
     6         int result = 0;
     7         for(int i = 0; i < grid.size(); i++) {
     8             for(int j = 0; j < grid[i].size(); j++) {
     9                 if(grid[i][j] == '1') {
    10                     grid[i][j] = '2';
    11                     bfs(grid, i, j);
    12                     result++;
    13                 }
    14             }
    15         }
    16         return result;
    17     }
    18     
    19     void bfs(vector<vector<char> >& grid, int i, int j) {
    20         queue<pair<int, int> > qu;
    21         qu.push(make_pair(i, j));
    22         
    23         int m = grid.size(), n = grid[0].size();
    24         while(!qu.empty()) {
    25             pair<int, int> node = qu.front();
    26             i = node.first;
    27             j = node.second;
    28             qu.pop();
    29             // check whether its neighbour is island
    30             if(i > 0 && grid[i - 1][j] == '') {
    31                 qu.push(make_pair(i - 1, j));
    32                 grid[i - 1][j] = '2';
    33             }
    34             if(j > 0 && grid[i][j - 1] == '1') {
    35                 qu.push(make_pair(i, j - 1));
    36                 grid[i][j - 1] = '2';
    37             }
    38             if(j < n - 1 && grid[i][j + 1] == '1') {
    39                 qu.push(make_pair(i, j + 1));
    40                 grid[i][j + 1] = '2';
    41             }
    42             if(i < m - 1 && grid[i + 1][j] == '1') {
    43                 qu.push(make_pair(i + 1, j));
    44                 grid[i + 1][j] = '2';
    45             }
    46         }
    47     }
    48 };
    View Code

    Runtime: Recursive 8ms.

     1 class Solution {
     2 public:
     3     int numIslands(vector<vector<char>>& grid) {
     4         int result;
     5         if(grid.empty() || grid[0].empty()) return result;
     6         
     7         int row = grid.size(), col = grid[0].size();
     8         for(int i = 0; i < row; i++){
     9             for(int j = 0; j < col; j++){
    10                 if(grid[i][j] == '1'){
    11                     helper(grid, i, j);
    12                     result++;
    13                 }
    14             }
    15         }
    16         return result;
    17     }
    18     
    19     void helper(vector<vector<char> > &grid, int x, int y){
    20         if(x >= grid.size() || x < 0 || y >= grid[0].size() || y < 0 || grid[x][y] != '1')
    21             return;
    22             
    23         grid[x][y] = '2';
    24         helper(grid, x + 1, y);
    25         helper(grid, x - 1, y);
    26         helper(grid, x, y + 1);
    27         helper(grid, x, y - 1);
    28     }
    29 };
  • 相关阅读:
    有关Lucene的问题(7):用Lucene构建实时的索引
    Lucene学习总结之九:Lucene的查询对象(1)
    有关Lucene的问题(6):Lucene的事务性
    Lucene学习总结之九:Lucene的查询对象
    面向连接的Socket Server的简单实现
    Lucene 原理与代码分析完整版
    有关Lucene的问题(8):用Lucene构建实时索引的文档更新问题
    k8spod的状态为evicted的情况分析
    centos7环境 的 k8s安装helm 3.7.1
    VMware虚拟机中CentOS7的硬盘空间扩容
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/5759327.html
Copyright © 2011-2022 走看看