本文主要是介绍LeetCode - 200. 岛屿数量,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
描述
给你一个由 '1'(陆地)和 '0'(水)组成的的二维网格,请你计算网格中岛屿的数量。
岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
此外,你可以假设该网格的四条边均被水包围。
示例 1:
输入:grid = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
]
输出:1
示例 2:
输入:grid = [
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]
输出:3
提示:
m == grid.length
n == grid[i].length
1 <= m, n <= 300
grid[i][j] 的值为 '0' 或 '1'
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-islands/
求解
class UnionFind {public:UnionFind(int n) : count(n) {parent.reserve(count + 1);for (int i = 0; i <= count; ++i) {parent.emplace_back(i);}rank.resize(count + 1, 1); // 初始每个的层级均为1}bool isConnected(int p, int q) {return find(p) == find(q);}void unionElements(int p, int q) {int proot = find(p);int qroot = find(q);if (proot == qroot) {return;}if (rank[proot] < rank[qroot]) {parent[proot] = qroot;} else if (rank[proot] > rank[qroot]) {parent[qroot] = proot;} else {// rank[proot] == rank[qroot]parent[proot] = qroot;++rank[qroot]; // proot ”挂载“到qroot下面,本来两个层级一致,现在需要增加1}}// 获取连通分量int getCount() {int connectCount = 0;for (int i = 0; i < count; ++i) {if (parent[i] == i) {++connectCount;}}return connectCount;}private:int find(int p) {while (p != parent[p]) {parent[p] = parent[parent[p]]; // 路径压缩优化,请细品p = parent[p];}return p;}private:std::vector<int> parent;int count;std::vector<int> rank;};class Solution {public:// 方法一,深度优先遍历求连通分量int numIslands_1e(vector<vector<char>> &grid) {m = grid.size();n = grid[0].size();int count = 0;visited.assign(m, vector<bool>(n, false));for (int i = 0; i < m; ++i) {for (int j = 0; j < n; ++j) {if (grid[i][j] == '0' || visited[i][j]) {continue;}++count;dfs(grid, i, j);}}return count;}// 方法一,广度优先遍历求连通分量int numIslands_2e(vector<vector<char>> &grid) {m = grid.size();n = grid[0].size();int count = 0;std::queue<std::pair<int, int>> q;visited.assign(m, vector<bool>(n, false));for (int i = 0; i < m; ++i) {for (int j = 0; j < n; ++j) {if (grid[i][j] == '0' || visited[i][j]) {continue;}++count;q.emplace(i, j);visited[i][j] = true;while (!q.empty()) {auto[r, c] = q.front(); // c++17新增结构化绑定特性q.pop();for (const auto &p : directions) {int newR = r + p.first;int newC = c + p.second;if (newR < 0 || newR >= m || newC < 0 || newC >= n) {// 索引超范围,跳过当前循环continue;}// 访问未访问过且为0的节点if (!visited[newR][newC] && grid[newR][newC] == '1') {dfs(grid, newR, newC);}}}}}return count;}// 方法三,并查集求连通分量// 所有的0关联,1各自关联int numIslands(vector<vector<char>> &grid) {m = grid.size();n = grid[0].size();const int gridNum = m * n;// 所有0与该值关联, 该值一定大于所有格子的编号,格子编号[0...gridNum-1]UnionFind uf(gridNum + 1);for (int i = 0; i < m; ++i) {for (int j = 0; j < n; ++j) {int id = i * n + j;if (grid[i][j] == '0') {uf.unionElements(id, gridNum);continue;}// grid[i][j] == '1'if (i < m - 1 && grid[i + 1][j] == '1') {// bottomuf.unionElements(id, id + n);}if (j < n - 1 && grid[i][j + 1] == '1') {// rightuf.unionElements(id, id + 1);}}}return uf.getCount() - 1;}private:vector<vector<bool>> visited;const vector<std::pair<int, int>> directions{{1, 0},{0, 1},{-1, 0},{0, -1}};int m = 0;int n = 0;// 深度遍历网格void dfs(const vector<vector<char>> &grids, int r, int c) {visited[r][c] = true;for (const auto &p : directions) {int newR = r + p.first;int newC = c + p.second;if (newR < 0 || newR >= m || newC < 0 || newC >= n) {// 索引超范围,跳过当前循环continue;}// 访问未访问过且为0的节点if (!visited[newR][newC] && grids[newR][newC] == '1') {dfs(grids, newR, newC);}}}};
这篇关于LeetCode - 200. 岛屿数量的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!