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: As I saw the problem, I feel that I can use dfs to search all continuous '1's and mark them. Scanning row by row, from column to column, when meeting a '1', then mark all its continuous block of '1's. After that, all of its connecting '1's are labeled. Then find the next '1', and mark its connecting '1's, until the right bottom item. 

    So I add a visited vector vector to label the visited items. But it exceeded time limite. 

     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         vector<vector<bool> > visited(row, vector<bool>(col, false));
     9         for(int i = 0; i < row; i++){
    10             for(int j = 0; j < col; j++){
    11                 if(grid[i][j] == '1' && !visited[i][j]){
    12                     helper(grid, visited, i, j);
    13                     result++;
    14                 }
    15             }
    16         }
    17         return result;
    18     }
    19     
    20     void helper(vector<vector<char> > grid, vector<vector<bool> >& visited, int currentX, int currentY){
    21         if(currentX >= grid.size() || currentX < 0 || currentY >= grid[0].size() || currentY < 0 || 
    22            grid[currentX][currentY] == '0' || visited[currentX][currentY])
    23            return;
    24         
    25         visited[currentX][currentY] = true;
    26         helper(grid, visited, currentX + 1, currentY);
    27         helper(grid, visited, currentX - 1, currentY);
    28         helper(grid, visited, currentX, currentY + 1);
    29         helper(grid, visited, currentX, currentY - 1);
    30     }
    31 };
    View Code

    This version deletes the visited vector vector because we can simply replace '1' by any non-0 or non-1 value. It passed with 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 };
  • 相关阅读:
    Smobiler如何实现.net一键开发,ios和android跨平台运行
    使用Smobiler实现类似美团的界面
    疫情当下,企业系统如何快速实现移动化?
    Smobiler针对百度文字识别SDK动态编译与运行
    smobiler自适应不同手机分辨率
    仓库管理移动应用解决方案——C#开发的移动应用开源解决方案
    移动OA办公——Smobiler第一个开源应用解决方案,快来get吧
    react-native 标题随页面滚动显示和隐藏
    react-native 键盘遮挡输入框
    解决adb网络连接中出现的“由于目标计算机积极拒绝,无法连接”错误
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/5209143.html
Copyright © 2011-2022 走看看