leetcode-200. 岛屿数量-并查集

这篇博客介绍了如何利用并查集数据结构解决LeetCode第200题——岛屿数量的问题。通过二维数组降维和有秩的并查集实现,遍历网格中的每个元素,当遇到陆地时进行连通性判断和合并,最终得到岛屿的数量。博客详细展示了代码实现过程以及每个步骤的解释,包括并查集的find和unite操作。
摘要由CSDN通过智能技术生成

leetcode-200. 岛屿数量

给你一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量。
岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
此外,你可以假设该网格的四条边均被水包围。

并查集(Union-find Data Structure)是一种树型的数据结构。
它的特点是由子结点找到父亲结点,用于处理一些不交集(Disjoint Sets)的合并及查询问题。

Find:确定元素属于哪一个子集。它可以被用来确定两个元素是否属于同一子集。
Union:将两个子集合并成同一个集合。

代码实现

1 二维数组降维
2 使用有秩的并查集
3 合并时,只需要遍历两个方向即可实现整个岛的连通性遍历

class UnionFind {
public:
    UnionFind(vector<vector<char>>& grid) {
        count = 0;
        int m = grid.size();
        int n = grid[0].size();
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (grid[i][j] == '1') {
					// 二位数组一维化
                    parent.push_back(i * n + j);
					// 最开始有 count 个集合,然后在 unite 中不断合并,不断递减
                    ++count;
                }
                else {
                    parent.push_back(-1);
                }
                rank.push_back(0);
            }
        }
    }

    int find(int i) {
        if (parent[i] != i) {
            parent[i] = find(parent[i]);
        }
        return parent[i];
    }

    void unite(int x, int y) {
        int rootx = find(x);
        int rooty = find(y);
        if (rootx != rooty) {
			// rootx是等级高或等级相等的那个
			// 因为要让 rooty 臣服于 rootx
            if (rank[rootx] < rank[rooty]) {
                swap(rootx, rooty);
            }
            parent[rooty] = rootx;
            if (rank[rootx] == rank[rooty])
				rank[rootx] += 1;

            --count;
        }
    }

    int getCount() const {
        return count;
    }

private:
    vector<int> parent; // 顶部节点的父节点就是自己
    vector<int> rank; // 等级
    int count;
};

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

        UnionFind uf(grid);

        for (int i = 0; i < row; ++i) {
            for (int j = 0; j < col; ++j) {
                if (grid[i][j] == '1') {

					if ((i + 1 < row) && (grid[i + 1][j] == '1')) {
						uf.unite(i * col + j, (i + 1) * col + j);
					}

					if ((j + 1 < col) && (grid[i][j + 1] == '1')) {
						uf.unite(i * col + j, i * col + j + 1);
					}

                }
            }
        }

        return uf.getCount();
    }
};

实例

以下表为例:
j 0 j 1 j 2
i 0 [“1”,“1”,“0”]
i 1 [“1”,“1”,“1”]
i 2 [“0”,“1”,“1”]

变成
parent:
0 1 -1 3 4 5 -1 7 8

rank:
0 0 0 0 0 0 0 0 0

【对于(0,0)位置】
合并 (0,0) 和 (0,1)时

void unite(x = 0, y = 1) {
    rootx = find(x) = 0;
    rooty = find(y) = 1;
    if (rootx != rooty) {
		// rank[0] == rank[1],不进入如下的 if
        if (rank[rootx] < rank[rooty]) {
            swap(rootx, rooty);
        }
		
		// parent[3] = 0;
        parent[rooty] = rootx;
        if (rank[rootx] == rank[rooty])
			rank[rootx] += 1;
		// rank[0] = 1
        --count;
    }
}

parent:
0 0 -1 3 4 5 -1 7 8

rank:
1 0 0 0 0 0 0 0 0

合并 (0,0) 和 (1,0)时

void unite(x = 0, y = 3) {
    rootx = find(x) = 0;
    rooty = find(y) = 3;
    if (rootx != rooty) {
		// rank[0] > rank[3],不进入如下的 if
        if (rank[rootx] < rank[rooty]) {
            swap(rootx, rooty);
        }
		
		// parent[3] = 0;
        parent[rooty] = rootx;
        if (rank[rootx] == rank[rooty])
			rank[rootx] += 1;
        --count;
    }
}

parent:
0 0 -1 0 4 5 -1 7 8

rank:
1 0 0 0 0 0 0 0 0

【对于(0,1)位置】
合并 (0,1) 和 (1,1)时

void unite(x = 1, y = 4) {
    rootx = find(x) = 0;
    rooty = find(y) = 4;
    if (rootx != rooty) {
		// rank[0] > rank[4],不进入如下的 if
        if (rank[rootx] < rank[rooty]) {
            swap(rootx, rooty);
        }
		
		// parent[4] = 0;
        parent[rooty] = rootx;
        if (rank[rootx] == rank[rooty])
			rank[rootx] += 1;
        --count;
    }
}

parent:
0 0 -1 0 0 5 -1 7 8

rank:
1 0 0 0 0 0 0 0 0

【对于(1,0)位置】
合并 (1,0) 和 (1,1)时

void unite(x = 3, y = 4) {
    rootx = find(x) = 0;
    rooty = find(y) = 0;
	// 不做处理
}

【对于(1,1)位置】
合并 (1,1) 和 (1,2)时

void unite(x = 4, y = 5) {
    rootx = find(x) = 0;
    rooty = find(y) = 5;
    if (rootx != rooty) {
		// rank[0] > rank[5],不进入如下的 if
        if (rank[rootx] < rank[rooty]) {
            swap(rootx, rooty);
        }
		
		// parent[5] = 0;
        parent[rooty] = rootx;
        if (rank[rootx] == rank[rooty])
			rank[rootx] += 1;
        --count;
    }
}

parent:
0 0 -1 0 0 0 -1 7 8

rank:
1 0 0 0 0 0 0 0 0

合并 (1,1) 和 (2,1)时

void unite(x = 4, y = 7) {
    rootx = find(x) = 0;
    rooty = find(y) = 7;
    if (rootx != rooty) {
		// rank[0] > rank[7],不进入如下的 if
        if (rank[rootx] < rank[rooty]) {
            swap(rootx, rooty);
        }
		
		// parent[7] = 0;
        parent[rooty] = rootx;
        if (rank[rootx] == rank[rooty])
			rank[rootx] += 1;
        --count;
    }
}

parent:
0 0 -1 0 0 0 -1 0 8

rank:
1 0 0 0 0 0 0 0 0

【对于(1,2)位置】
合并 (1,2) 和 (2,2)时

void unite(x = 5, y = 8) {
    rootx = find(x) = 0;
    rooty = find(y) = 8;
    if (rootx != rooty) {
		// rank[0] > rank[8],不进入如下的 if
        if (rank[rootx] < rank[rooty]) {
            swap(rootx, rooty);
        }
		
		// parent[8] = 0;
        parent[rooty] = rootx;
        if (rank[rootx] == rank[rooty])
			rank[rootx] += 1;
        --count;
    }
}

parent:
0 0 -1 0 0 0 -1 0 0

rank:
1 0 0 0 0 0 0 0 0

可以看到最后0的秩为1,且1、3、4、5、7、8的parent都是0,即(0,0)

让合并接口清晰一点

    void unite(int x, int y) {
        int rootx = find(x);
        int rooty = find(y);
        if (rootx != rooty) {
			// rootx是等级高或等级相等的那个
			// 因为要让 rooty 臣服于 rootx
            if (rank[rootx] < rank[rooty]) {
				parent[rootx] = rooty;
            } else if (rank[rootx] > rank[rooty]) {
				parent[rooty] = rootx;
			} else {
				parent[rooty] = rootx;
				rank[rootx] += 1;
			}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值