LeetCode 题解(131): 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

题解:

用DFS,每当访问到一个未曾访问过的'1'时,count加一,同时DFS周围的'1',标记为visited。最后返回count。

C++版:

class Solution {
public:
    int row, col;
    int numIslands(vector<vector<char>>& grid) {
        if(grid.size() == 0)
            return 0;
        row = grid.size();
        col = grid[0].size();
        vector<vector<bool>> visited(row, vector<bool>(col, false));
        int count = 0;
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                if(grid[i][j] == '1' && !visited[i][j]) {
                    count++;
                    mark(i, j, grid, visited);
                }
            }
        }
        return count;
    }
    
    void mark(int i, int j, vector<vector<char>>& grid, vector<vector<bool>>& visited) {
        if(i < 0 || j < 0 || i >= row || j >= col || visited[i][j])
            return;
        visited[i][j] = true;
        if(grid[i][j] == '1') {
            
            mark(i+1, j, grid, visited);
            mark(i-1, j, grid, visited);
            mark(i, j+1, grid, visited);
            mark(i, j-1, grid, visited);
        }
    }
};

Java版:

public class Solution {
    public int row;
    public int col;
    public int numIslands(char[][] grid) {
        if(grid.length == 0)
            return 0;
        row = grid.length;
        col = grid[0].length;
        int count = 0;
        boolean[][] visited = new boolean[row][col];
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                if(grid[i][j] == '1' && !visited[i][j]) {
                    count++;
                    mark(i, j, grid, visited);
                }
            }
        }
        return count;
    }
    
    public void mark(int i, int j, char[][] grid, boolean[][] visited) {
        if(i < 0 || j < 0 || i >= row || j >= col || visited[i][j])
            return;
        if(grid[i][j] == '1') {
            visited[i][j] = true;
            mark(i+1, j, grid, visited);
            mark(i-1, j, grid, visited);
            mark(i, j+1, grid, visited);
            mark(i, j-1, grid, visited);
        }
    }
}

Python版:

class Solution:
    # @param {character[][]} grid
    # @return {integer}
    def numIslands(self, grid):
        if len(grid) == 0:
            return 0
        
        visited = [[False for i in range(len(grid[0]))] for j in range(len(grid))]
        count = 0
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                if grid[i][j] == '1' and not visited[i][j]:
                    count += 1
                    self.mark(i, j, grid, visited)
        return count
        
    def mark(self, i, j, grid, visited):
        if i >= len(grid) or i < 0 or j >= len(grid[0]) or j < 0 or visited[i][j]:
            return
        visited[i][j] = True
        if grid[i][j] == '1':
            self.mark(i+1, j, grid, visited)
            self.mark(i, j+1, grid, visited)
            self.mark(i-1, j, grid, visited)
            self.mark(i, j-1, grid, visited)
                
                        


你可能感兴趣的:(Algorithm,LeetCode,面试题)