Rayman的绝顶之路——Leetcode每日一题打卡6
2020-04-21 23:46
429 查看
Leetcode200. 岛屿数量 题目:
给你一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量。
岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
此外,你可以假设该网格的四条边均被水包围。
示例 1:
输入:
11110
11010
11000
00000
输出: 1
示例 2:
输入:
11000
11000
00100
00011
输出: 3
解释: 每座岛屿只能由水平和/或竖直方向上相邻的陆地连接而成。
思路1(DFS):
- 首先对传入的数组进行遍历,找到为1的字符;
- 从找到1的位置进行深度优先搜索,将已经遍历过的且为1的字符赋值为0表示已经遍历;
- 查找该字符的上下左右是否为1,进行递归,重复上述操作直至遍历结束。
Java AC代码如下:
public class Leetcode200 { //DFS static int numIslands(char[][] grid) { if (grid == null || grid.length == 0) { return 0; } int sum = 0; for (int i = 0; i < grid.length; ++i) { for (int j = 0; j < grid[0].length; ++j) { if (grid[i][j] == '1') { ++sum; dfs(grid, i, j); } } } return sum; } static void dfs(char[][] grid, int i, int j) { if (i < 0 || j < 0 || i >= grid.length || j >= grid[0].length || grid[i][j] == '0') { return; } grid[i][j] = '0'; dfs(grid, i - 1, j); dfs(grid, i + 1, j); dfs(grid, i, j - 1); dfs(grid, i, j + 1); } public static void main(String[] args) { char[][] c = { {'1', '1', '1', '1', '0'}, {'1', '1', '0', '1', '0'}, {'1', '1', '0', '0', '0'}, {'0', '0', '0', '0', '0'} }; System.out.println(numIslands(c)); } }
思路2(BFS):
- 为了求出岛屿的数量,我们可以扫描整个二维网格。如果一个位置为 1,则将其加入队列,开始进行广度优先搜索。
- 在广度优先搜索的过程中,每个搜索到的 1 都会被重新标记为 0 。直到队列为空,搜索结束。
Java AC代码如下:
import java.util.LinkedList; import java.util.Queue; public class Leetcode200 { //BFS static int numIslands(char[][] grid) { if (grid == null || grid.length == 0) { return 0; } int nr = grid.length; int nc = grid[0].length; int sum = 0; for (int r = 0; r < nr; ++r) { for (int c = 0; c < nc; ++c) { if (grid[r][c] == '1') { ++sum; grid[r][c] = '0'; Queue<Integer> neighbors = new LinkedList<>(); neighbors.add(r * nc + c); while (!neighbors.isEmpty()) { int id = neighbors.remove(); int row = id / nc; int col = id % nc; if (row - 1 >= 0 && grid[row - 1][col] == '1') { neighbors.add((row - 1) * nc + col); grid[row - 1][col] = '0'; } if (row + 1 < nr && grid[row + 1][col] == '1') { neighbors.add((row + 1) * nc + col); grid[row + 1][col] = '0'; } if (col - 1 >= 0 && grid[row][col - 1] == '1') { neighbors.add(row * nc + col - 1); grid[row][col - 1] = '0'; } if (col + 1 < nc && grid[row][col + 1] == '1') { neighbors.add(row * nc + col + 1); grid[row][col + 1] = '0'; } } } } } return sum; } public static void main(String[] args) { char[][] c = { {'1', '1', '1', '1', '0'}, {'1', '1', '0', '1', '0'}, {'1', '1', '0', '0', '0'}, {'0', '0', '0', '0', '0'} }; System.out.println(numIslands(c)); } }
思路3(并查集):
查看了官方的题解给了我新的思路!!!
- 扫描整个二维网格。如果一个位置为 1 ,则将其与相邻四个方向上的 1 在并查集中进行合并。
- 最终岛屿的数量就是并查集中连通分量的数目。
Java AC代码如下:
//参考官方解答 public class Leetcode200 { //并查集 static class DSU { int count; int[] parent; int[] rank; public DSU(char[][] grid) { count = 0; parent = new int[grid.length * grid[0].length]; rank = new int[grid.length * grid[0].length]; for (int i = 0; i < grid.length; ++i) { for (int j = 0; j < grid[0].length; ++j) { if (grid[i][j] == '1') { parent[i * grid[0].length + j] = i * grid[0].length + j; ++count; } rank[i * grid[0].length + j] = 0; } } } public int find(int i) { if (parent[i] != i) parent[i] = find(parent[i]); return parent[i]; } public void union(int x, int y) { int rootx = find(x); int rooty = find(y); if (rootx != rooty) { if (rank[rootx] > rank[rooty]) { parent[rooty] = rootx; } else if (rank[rootx] < rank[rooty]) { parent[rootx] = rooty; } else { parent[rooty] = rootx; rank[rootx] += 1; } --count; } } public int getCount() { return count; } } static int numIslands(char[][] grid) { if (grid == null || grid.length == 0) { return 0; } int nr = grid.length; int nc = grid[0].length; int num_islands = 0; DSU uf = new DSU(grid); for (int r = 0; r < nr; ++r) { for (int c = 0; c < nc; ++c) { if (grid[r][c] == '1') { grid[r][c] = '0'; if (r - 1 >= 0 && grid[r - 1][c] == '1') { uf.union(r * nc + c, (r - 1) * nc + c); } if (r + 1 < nr && grid[r + 1][c] == '1') { uf.union(r * nc + c, (r + 1) * nc + c); } if (c - 1 >= 0 && grid[r][c - 1] == '1') { uf.union(r * nc + c, r * nc + c - 1); } if (c + 1 < nc && grid[r][c + 1] == '1') { uf.union(r * nc + c, r * nc + c + 1); } } } } return uf.getCount(); } public static void main(String[] args) { char[][] c = { {'1', '1', '1', '1', '0'}, {'1', '1', '0', '1', '0'}, {'1', '1', '0', '0', '0'}, {'0', '0', '0', '0', '0'} }; System.out.println(numIslands(c)); } }
2020.4.20打卡
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Rayman的绝顶之路——Leetcode每日一题打卡7
- 【每日打卡】Day15:最佳加法表达式(大数运算)JAVA实现(部分超时)
- 2018.04.01 leetcode 打卡
- LeetCode高频面试60天打卡日记Day01
- 每日打卡 | 5月12日教资&教招每日一练
- 【leetcode每日一题】100.same treet
- leetcode:58 Length of Last Word-每日编程第三十四题
- 每日打卡 2017.03.12 POJ题目分类 初级-一、基本算法
- LeetCode每日一题--Excel表列序号
- LeetCode每日一题--最大子序和
- 每日一题:LeetCode之括号生成
- leetcode:67 Add Binary-每日编程第四十三题
- 【每日打卡】Day27:三数之和 C++实现
- 每日一恋 - LeetCode 328. Odd Even Linked List(奇偶链表)
- 每日AC - 后缀表达式计算结果-leetcode-evaluate-reverse-polish-notation
- leetcode 每日一题 217. Contains Duplicate
- LeetCode每日一题 010 无重复字符的最长子串
- LeetCode每日一题——136. Single Number
- leetcode 打卡 2018.03.06 #766 Toepliz Matrix
- 2018.03.07 leetcode 打卡 #169. Majority Element