-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode200.cpp
235 lines (218 loc) · 6.76 KB
/
leetcode200.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*************************************************
Author: wenhaofang
Date: 2022-11-12
Description: leetcode200 - Number of Islands
*************************************************/
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <algorithm>
#include <math.h>
using std::vector;
using std::string;
using std::unordered_map;
using std::unordered_set;
using std::stack;
using std::queue;
using std::priority_queue;
using std::max;
using std::min;
using std::swap;
using std::pair;
using std::cin;
using std::cout;
using std::endl;
/**
* 方法一:深度优先搜索
*
* 理论时间复杂度:O(m * n),其中 m、n 分别为矩阵的宽和长
* 理论空间复杂度:O(m * n),其中 m、n 分别为矩阵的宽和长
*
* 实际时间复杂度:Runtime: 50 ms, faster than 80.17% of C++ online submissions
* 实际空间复杂度:Memory Usage: 12.3 MB, less than 61.43% of C++ online submissions
*/
// class Solution {
// public:
// void dfs(vector<vector<char>>& grid, int x, int y, int m, int n) {
// if (
// x < 0 ||
// y < 0 ||
// x >= m ||
// y >= n ||
// grid[x][y] != '1'
// ) {
// return;
// }
// grid[x][y] = '0';
// dfs(grid, x + 1, y, m, n);
// dfs(grid, x - 1, y, m, n);
// dfs(grid, x, y + 1, m, n);
// dfs(grid, x, y - 1, m, n);
// }
// int numIslands(vector<vector<char>>& grid) {
// int m = grid.size();
// int n = grid[0].size();
// // 遍历矩阵
// // 发现陆地之后,岛屿数量加一,并使用深度优先搜索将相邻的陆地标记
// int ans = 0;
// for (int i = 0; i < m; i++) {
// for (int j = 0; j < n; j++) {
// if (grid[i][j] == '1') {
// ans += 1;
// dfs(grid, i, j, m, n);
// }
// }
// }
// return ans;
// }
// };
/**
* 方法二:广度优先搜索
*
* 理论时间复杂度:O(m * n),其中 m、n 分别为矩阵的宽和长
* 理论空间复杂度:O(m * n),其中 m、n 分别为矩阵的宽和长
*
* 实际时间复杂度:Runtime: 52 ms, faster than 78.66% of C++ online submissions
* 实际空间复杂度:Memory Usage: 18.2 MB, less than 19.77% of C++ online submissions
*/
// class Solution {
// public:
// const int dx[4] = {1, -1, 0, 0};
// const int dy[4] = {0, 0, 1, -1};
// void bfs(vector<vector<char>>& grid, int x, int y, int m, int n) {
// queue<pair<int, int>> q;
// grid[x][y] = '0';
// q.emplace(x, y);
// while (!q.empty()) {
// int nx = q.front().first;
// int ny = q.front().second;
// q.pop();
// for (int k = 0; k < 4; k++) {
// int mx = nx + dx[k];
// int my = ny + dy[k];
// if (
// mx < 0 ||
// my < 0 ||
// mx >= m ||
// my >= n ||
// grid[mx][my] != '1'
// ) {
// continue;
// }
// grid[mx][my] = '0';
// q.emplace(mx, my);
// }
// }
// }
// int numIslands(vector<vector<char>>& grid) {
// int m = grid.size();
// int n = grid[0].size();
// // 遍历矩阵
// // 发现陆地之后,岛屿数量加一,并使用广度优先搜索将相邻的陆地标记
// int ans = 0;
// for (int i = 0; i < m; i++) {
// for (int j = 0; j < n; j++) {
// if (grid[i][j] == '1') {
// ans += 1;
// bfs(grid, i, j, m, n);
// }
// }
// }
// return ans;
// }
// };
/**
* 方法三:并查集
*
* 理论时间复杂度:O(m * n),其中 m、n 分别为矩阵的宽和长
* 理论空间复杂度:O(m * n),其中 m、n 分别为矩阵的宽和长
*
* 实际时间复杂度:Runtime: 42 ms, faster than 88.12% of C++ online submissions
* 实际空间复杂度:Memory Usage: 15.8 MB, less than 28.37% of C++ online submissions
*/
class UnionFind {
private:
vector<int> parent;
vector<int> rank;
int count;
public:
UnionFind(vector<vector<char>>& grid) {
int m = grid.size();
int n = grid[0].size();
count = 0;
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++;
}
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) {
if (rank[rootx] < rank[rooty]) {
swap(rootx, rooty);
}
parent[rooty] = rootx;
if (rank[rootx] == rank[rooty]) {
rank[rootx] += 1;
}
count--;
}
}
int getCount() {
return count;
}
};
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int m = grid.size();
int n = grid[0].size();
UnionFind uf(grid);
int ans = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
grid[i][j] = '0';
if (i + 1 < m && grid[i + 1][j] == '1') uf.unite(i * n + j, (i + 1) * n + j);
if (i - 1 >= 0 && grid[i - 1][j] == '1') uf.unite(i * n + j, (i - 1) * n + j);
if (j + 1 < n && grid[i][j + 1] == '1') uf.unite(i * n + j, i * n + (j + 1));
if (j - 1 >= 0 && grid[i][j - 1] == '1') uf.unite(i * n + j, i * n + (j - 1));
}
}
}
return uf.getCount();
}
};
/**
* 测试
*/
int main() {
Solution* solution = new Solution();
vector<vector<char>> grid = {
{'1', '1', '1', '1', '0'},
{'1', '1', '0', '1', '0'},
{'1', '1', '0', '0', '0'},
{'0', '0', '0', '0', '0'},
};
int ans = solution -> numIslands(grid);
cout << ans << endl;
}