题目
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:
Input:
11110
11010
11000
00000
Output: 1
Example 2:
Input:
11000
11000
00100
00011
Output: 3
分析
- 这个题目特别好,基本能把很多个算法都练习一遍;
- 这个题目可以深刻理解DFS和BFS的思路。
- 分析:就是求有连通的区域的数目,认为1是连通的标记;首先想到的就是并查集;其次这是个搜索的问题,DFS和BFS都可以解决。
- 算法对比(k是‘1’的数量)
| 算法 | 时间复杂度 | 空间复杂度 | 最坏空间复杂度 |
| 并查集 | O(r * c) | O(r * c) |O(r * c) |
| DFS | O(r * c) | O(k) | O(r * c) |
| BFS | O(r * c) | O(min(r, c)) | O(min(r, c)) |
DFS和BFS的最坏情况是全部区域均为石头1.
BFS的空间复杂度就是 队列的长度;刚开始以为是max(r,c),实际在纸上画一下,发现真是min(r,c);
并查集
- quickUnion的并查集;注意:每次union时直接连接另一个树的root,可以减少树的高度。
class Solution {
private class UnionFind {
private int[] union;
private int num;
public UnionFind(int num) {
if (num <= 0) throw new IllegalArgumentException("num需要大于0");
this.num = num;
this.union = new int[num];
for (int i = 0; i < num; i++) {
union[i] = i;
}
}
private int root(int i) {
while (i != union[i]) {
i = union[i];
}
return i;
}
public void union(int x, int y) {
if (sameRoot(x, y)) return;
num--;
// System.out.println(x + ":" + y);
union[root(y)] = root(x);
}
public boolean sameRoot(int x, int y) {
return root(x) == root(y);
}
public int getNum() {
return num;
}
}
public int numIslands(char[][] grid) {
int row = grid.length;
if (row == 0) return 0;
int col = grid[0].length;
if (col == 0) return 0;
UnionFind unionFind = new UnionFind(row * col);
int zero = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (grid[i][j] == '0') {
zero++;
continue;
}
int index = i * col + j;
if (i > 0 && grid[i - 1][j] == '1') {
unionFind.union(index, (i - 1) * col + j);
}
if (j > 0 && grid[i][j - 1] == '1') {
unionFind.union(index, i * col + (j - 1));
}
}
}
return unionFind.getNum() - zero;
}
}
搜索DFS
- DFS:注意优化的思路,刚开始的答案 运行时间比较长,因为使用了一个hashSet来记录搜索过的路径。
- hasSearch的条件有个技巧:可以修改grid的值,来代替hashSet来标记搜索过的区域。
class Solution {
private char[][] grid;
private Set<String> has = new HashSet<String>();
public void dfs(int x, int y) {
if (grid[x][y] == '0') return;
// if (has.contains(x + "#" + y)) return;
// has.add(x + "#" + y);
grid[x][y] = '0';
if (x > 0) dfs(x - 1, y);
if (y > 0) dfs(x, y - 1);
if (x < grid.length - 1) dfs(x + 1, y);
if (y < grid[0].length - 1) dfs(x, y + 1);
}
public int numIslands(char[][] grid) {
int row = grid.length;
if (row == 0) return 0;
int col = grid[0].length;
if (col == 0) return 0;
this.grid = grid;
int num = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (grid[i][j] == '0') continue;
if (!has.contains(i + "#" + j)) {
num++;
dfs(i, j);
}
}
}
return num;
}
}
搜索BFS
- BFS:使用queue记录需要搜索的路径
public class P200_numIslands {
private char[][] grid;
private Set<String> has = new HashSet<String>();
private int convert(int x, int y) {
return x * grid[0].length + y;
}
public void bfs(int x, int y) {
Queue<Integer> queue = new LinkedList<>();
if (grid[x][y] == '0') return;
((LinkedList<Integer>) queue).addLast(convert(x, y));
grid[x][y] = '0';
while (!queue.isEmpty()) {
int ele = ((LinkedList<Integer>) queue).pollLast();
// int first = Integer.parseInt(ele.substring(0, ele.indexOf("#")));
// int second = Integer.parseInt(ele.substring(ele.indexOf("#") + 1));
int first = ele / grid[0].length;
int second = ele - first * grid[0].length;
int tmp = convert(first - 1, second);
if (first > 0 && grid[first - 1][second] == '1') {
// has.add(tmp);
((LinkedList<Integer>) queue).addLast(tmp);
grid[first - 1][second] = '0';
}
tmp = convert(first + 1, second);
if (first < grid.length - 1 && grid[first + 1][second] == '1') {
// has.add(tmp);
((LinkedList<Integer>) queue).addLast(tmp);
grid[first + 1][second] = '0';
}
tmp = convert(first, second - 1);
if (second > 0 && grid[first][second - 1] == '1') {
// has.add(tmp);
((LinkedList<Integer>) queue).addLast(tmp);
grid[first][second - 1] = '0';
}
tmp = convert(first, second + 1);
if (second < grid[0].length - 1 && grid[first][second + 1] == '1') {
// has.add(tmp);
((LinkedList<Integer>) queue).addLast(tmp);
grid[first][second + 1] = '0';
}
}
}
public int numIslands(char[][] grid) {
int row = grid.length;
if (row == 0) return 0;
int col = grid[0].length;
if (col == 0) return 0;
this.grid = grid;
int num = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (grid[i][j] == '0') continue;
num++;
bfs(i, j);
}
}
return num;
}
}