• 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 };
  • 相关阅读:
    个人破解汇总
    JavaScript学习中的挑战
    18个黑白配色网页设计
    正则表达式常用用法汇总 __西科大C语言
    JS正则表达式大全(整理详细且实用)
    印象最深的三个老师
    pbzip2
    集群接口机存储监控
    vue...扩展符报错
    框架mpvue创建一个小程序
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/5759327.html
Copyright © 2020-2023  润新知