leetcode 200. 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

这个题有两种方法:

way-1:左上角开始找点,如果是1,就广度遍历(BFS)或者深度遍历(DFS)这个island的点,全部设为2

 然后ret++;


way-2:使用并查集(union find),其实这种找集合的题使用并查集是最简单的。这两种方法都必须掌握。


way-1代码

struct POS
{
    int x;
    int y;
    POS(int xx, int yy):x(xx), y(yy){}
};

class Solution 
{
public:
    void BFS(vector<vector<char>>& board, int i, int j)  //广度遍历 队列
    {
        int a[4] = {1, 0, -1, 0};
        int b[4] = {0, 1, 0, -1};
        
        queue<POS*> pos;
        pos.push(new POS(i,j));
        board[i][j] = '2';
        
        while(!pos.empty())
        {
            POS *front = pos.front();
            pos.pop();
            for (int i = 0; i < 4; i++)
            {
                if (isvalid(front->x + a[i], front->y + b[i]) && board[front->x + a[i]][front->y + b[i]] == '1')
                {
                    pos.push(new POS(front->x + a[i], front->y + b[i]));    
                    board[front->x + a[i]][front->y + b[i]] = '2';
                }
            }
        }
    }
    
    void DFS(vector<vector<char>>& board, int i, int j)  //深度遍历 栈
    {
        int a[4] = {1, 0, -1, 0};
        int b[4] = {0, 1, 0, -1};
        
        stack<POS*> pos;
        pos.push(new POS(i, j));
        board[i][j] = '2';
        bool isfind;  //由于在当前点周边找到新的符合要求的点就应该continue,但是在while里面还有一层for,continue无法开始新的while,所以需要一个isfind到外面去continue
        
        while (!pos.empty())
        {
            POS *front = pos.top(); 
            isfind = false;
            for (int i = 0; i < 4; i++)
            {
                if (isvalid(front->x + a[i], front->y + b[i]) && board[front->x + a[i]][front->y + b[i]] == '1')
                {
                    pos.push(new POS(front->x + a[i], front->y + b[i]));    
                    board[front->x + a[i]][front->y + b[i]] = '2';
                    isfind = true;
                    break;
                }
            }
            if (isfind)
                continue;
            pos.pop();
        }
    }
    
    bool isvalid(int i, int j)
    {
        return i >= 0 && i < row && j >=0 && j < col;
    }
    
    int numIslands(vector<vector<char>>& grid) 
    {
        int ret = 0;
        row = grid.size();
        if (row == 0)
          return ret;
          
        col = grid[0].size();
          
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                if (grid[i][j] == '1')
                {
                    ret++;
                    //BFS(grid, i, j);
                    DFS(grid, i, j);
                }
            }
        }
        return ret; 
    }
    
private:
    int row;
    int col;
};



way-2代码


class Solution 
{
public:  
    int numIslands(vector<vector<char>>& grid) 
    {
        row = grid.size();
        if (row == 0)   return 0;        
        col = grid[0].size();
        
        for (int i = 0; i < row * col; i++)
            father.push_back(i);
            
        int island = 0;
        int a[4] = {1, 0, -1, 0};
        int b[4] = {0, 1, 0, -1};
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                if (grid[i][j] == '1')
                {
                    island++;
                    for (int k = 0; k < 4; k++)
                    {
                        int _x = i + a[k];
                        int _y = j + b[k];
                        if (isvalid(_x, _y) && grid[_x][_y] == '1')
                        {
                            int father_1 = find_father(i * col + j);
                            int father_2 = find_father(_x * col + _y);
                            if (father_1 != father_2)
                            {
                                father[father_2] = father_1;
                                island--;
                            }
                        }
                    } 
                }
            }
        }
        return island; 
    }
    
private:
    int row;
    int col;
    vector<int> father;
    
    int find_father(int x)
    {
        if (father[x] == x)
            return x;
        return father[x] = find_father(father[x]);
    }
    
    bool isvalid(int i, int j)
    {
        return i >= 0 && i < row && j >=0 && j < col;
    }
};





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值