Leetcode 第200題:Number of Islands--島嶼的個數(C++、Python)

題目地址:Number of Islands


題目簡介:

給定一個由 '1'(陸地)和 '0'(水)組成的的二維網格,計算島嶼的數量。一個島被水包圍,並且它是通過水平方向或垂直方向上相鄰的陸地連接而成的。你可以假設網格的四個邊均被水包圍。

Example 1:

Input:
11110
11010
11000
00000
Output: 1

Example 2:

Input:
11000
11000
00100
00011
Output: 3

題目解析:

1、BFS

每第一次碰到'1',向四周發散把所有的相鄰的'1'找到,一個都不留。

C++(利用queue)

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        if(grid.size() == 0)
            return 0;
        int high = grid.size(), width = grid[0].size();
        int ans = 0;
        for (int i = 0; i < high; i++)
        {
            for (int j = 0; j < width; j++)
            {
                if (grid[i][j] == '1')
                {
                    ans++;
                    queue<int> pos;
                    pos.push(i), pos.push(j);
                    while(!pos.empty())
                    {
                        int x = pos.front();
                        pos.pop();
                        int y = pos.front();
                        pos.pop();
                        grid[x][y] = '0';
                        if (x + 1 < high && grid[x + 1][y] == '1')
                        {
                            pos.push(x + 1), pos.push(y);
                            grid[x + 1][y] = '0';
                        }
                        if (y + 1 < width && grid[x][y + 1] == '1')
                        {
                            pos.push(x), pos.push(y + 1);
                            grid[x][y + 1] = '0';
                        }
                        if (x - 1 > -1 && grid[x - 1][y] == '1')
                        {
                            pos.push(x - 1), pos.push(y);
                            grid[x - 1][y] = '0';
                        }
                        if (y - 1 > -1 && grid[x][y - 1] == '1')
                        {
                            pos.push(x), pos.push(y - 1);
                            grid[x][y - 1] = '0';
                        }
                    }
                }
            }
        }
        return ans;
    }
};

Python:

class Solution:
    def numIslands(self, grid: List[List[str]]) -> int:
        if len(grid) == 0:
            return 0
        high = len(grid)
        width = len(grid[0])
        ans = 0
        for i in range(high):
            for j in range(width):
                if grid[i][j] == '1':
                    ans += 1
                    pos = []
                    pos.append(j)
                    pos.append(i)
                    while(len(pos)):
                        x = pos.pop()
                        y = pos.pop()
                        grid[x][y] = '0'
                        if (x + 1 < high and grid[x + 1][y] == '1'):
                            pos.append(y)
                            pos.append(x + 1)
                            grid[x + 1][y] = '0'
                        if (y + 1 < width and grid[x][y + 1] == '1'):
                            pos.append(y + 1)
                            pos.append(x)
                            grid[x][y + 1] = '0'
                        if (x - 1 > -1 and grid[x - 1][y] == '1'):
                            pos.append(y)
                            pos.append(x - 1)
                            grid[x - 1][y] = '0'
                        if (y - 1 > -1 and grid[x][y - 1] == '1'):
                            pos.append(y - 1)
                            pos.append(x)
                            grid[x][y - 1] = '0'
        return ans

2、DFS

每次碰到第一次的'1',就先朝一個方向把'1'找完,一個都不剩。

C++:

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        if (grid.empty())
            return 0;
        int high = grid.size(), width = grid[0].size();
        int ans = 0;
        for (int i = 0; i < high; i++)
        {
            for (int j = 0; j < width; j++)
            {
                if (grid[i][j] == '0') 
                    continue;
                helper(grid, i, j);
                ans++;
            }
        }
        return ans;
    }
    void helper(vector<vector<char>>& grid, int x, int y) {
        if (x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size() || grid[x][y] == '0') 
            return;
        grid[x][y] = '0';
        helper(grid, x - 1, y);
        helper(grid, x + 1, y);
        helper(grid, x, y - 1);
        helper(grid, x, y + 1);
    }
};

Python:

class Solution:
    def numIslands(self, grid: List[List[str]]) -> int:
        if len(grid) == 0:
            return 0
        high = len(grid)
        width = len(grid[0])
        ans = 0
        for i in range(high):
            for j in range(width):
                if grid[i][j] == '0':
                    continue
                self.helper(grid, i, j)
                ans += 1
        return ans
    def helper(self, grid, x, y):
        if (x < 0 or x >= len(grid) or y < 0 or y >= len(grid[0]) or grid[x][y] == '0'):
            return
        grid[x][y] = '0'
        self.helper(grid, x - 1, y)
        self.helper(grid, x + 1, y)
        self.helper(grid, x, y - 1)
        self.helper(grid, x, y + 1)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章