Leetcode_UnionFind(level_M)_200_547

Description_200_number-of-islands

Description_547_friend-circles

Thinking

  • Something In Common
    • Both of two questions input a two-dimensional array, what’s more, them all need find and union operation.
    • If union sucessfully, number of islands or friend-circles minus 1.
  • Something Different
    • In the beginning, the number of islands equal to the number of ‘1’, however,the number of friend-circles equal to the row number of the array.
    • No.200 needs union surrounding area,while No.547 only union i and j.

Code

  • No.200

class UFset {
public:
    UFset(vector<vector<char>>& grid) {
        int row = grid.size();
        int col = grid[0].size();
        
        islandCnt = 0;
        parent = new int[row * col]();
        root = new int[row * col ] ();
        for (int i=0; i<row; i++) {
        	for (int j=0; j<col; j++) {
        		if (grid[i][j] == '1') {
        			parent[col*i + j] = 1;
        			islandCnt++;
				}
				root[col*i + j] = 1;
			}
        }
    }
    
    int uf_find(int x) {
        int x_root = x;
        while (!root[x_root]) x_root = parent[x_root];
        
        // compress path
        while (x_root != x) {
            int temp = parent[x];
            parent[x] = x_root;
            x = temp;
        }
        
        return x_root;
    }
    
    int uf_union(int x, int y) {
        int x_root = uf_find(x);
        int y_root = uf_find(y);
        
        if (x_root != y_root) {
            
            if (parent[x_root] < parent[y_root]) {
                parent[y_root] += parent[x_root];
                parent[x_root] = y_root;
                root[x_root] = 0;
            } else {
                parent[x_root] += parent[y_root];
                parent[y_root] = x_root;
                root[y_root] = 0;
            }
            
            islandCnt--;
            return 1;
        } else {
            return 0;
        }
    }
    
    int getCnt() {
        return islandCnt;
    }
    
private:
    int* parent;
    int* root;
    int islandCnt;
};

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
            int row = grid.size();
            if (!row) return 0;
            int col = grid[0].size();

            UFset uf(grid);
            for (int i=0; i<row; i++) {
                for (int j=0; j<col; j++) {
                    if (grid[i][j] == '1') {
                        grid[i][j] == '0';
                        if (i-1 >= 0 && grid[i-1][j] == '1') uf.uf_union(i*col+j, (i-1)*col+j);
                        if (i+1 < row && grid[i+1][j] == '1') uf.uf_union(i*col+j, (i+1)*col+j);
                        if (j-1 >= 0 && grid[i][j-1] == '1') uf.uf_union(i*col+j, i*col+j-1);
                        if (j+1 < col && grid[i][j+1] == '1') uf.uf_union(i*col+j, i*col+j+1);
                    }
                }
            }   

            return uf.getCnt();
    }
};
  • No.547
class UFset {
public:
    UFset(vector< vector<int> >& M) {
        int row = M.size();
        int col = M[0].size();
        
        parent = new int[row * col]();
        root = new int[row*col]();
        friendCircles = row;
        
        for (int i=0; i<row; i++) {
            for (int j=0; j<col; j++) {
                if (M[i][j] == 1) {
                    parent[i*col + j] = 1;
                }
                root[i*col + j] = 1;
            }
        } 
        
    }
    
    int uf_find(int x) {
        int x_root = x;
        while (!root[x_root]) {
            x_root = parent[x_root];
        }
        
        while (x_root != x) {
            int temp = parent[x];
            parent[x] = x_root;
            x = temp;
        }
        
        return x_root;
    }
    
    int uf_union(int x, int y) {
        int x_root = uf_find(x);
        int y_root = uf_find(y);
        if (x_root != y_root) {
            
            if (parent[x_root] < parent[y_root]) {
                parent[y_root] += parent[x_root];
                parent[x_root] = y_root;
                root[x_root] = 0;
            } else {
                parent[x_root] += parent[y_root];
                parent[y_root] = x_root;
                root[y_root] = 0;
            }
            
            friendCircles--;
            return 1;
        } else {
            return 0;
        }
    }
    
    int getCnt() {
        return friendCircles;
    }
    
private:
    int* parent;
    int* root;
    int friendCircles;
};

class Solution {
public:
    int findCircleNum(vector<vector<int>>& M) {
        int row = M.size();
        if (!row) return 0;
        int col = M[0].size();
        
        UFset uf(M);
        for (int i=0; i<row; i++) {
            for (int j=i; j<col; j++) {
                if (M[i][j] == 1) {
                    uf.uf_union(i, j);
                } 
            }
        }
        
        return uf.getCnt();
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值