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 };
  • 相关阅读:
    关于一个简单面试题(。net)
    写一个TT模板自动生成spring.net下面的配置文件。
    EF自动生成的(T4模板) 关联属性元数据修改
    关于面向切面编程的部分内容-错误处理机制
    memcache 安装
    Newtonsoft.Json工具类
    关于Memcache使用的工具类
    关于 Log4Net
    今天开始写博客
    OC 协议
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/5209143.html
Copyright © 2011-2022 走看看