leetcode刷题笔记【18】
类型:图的遍历
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’
//广度优先遍历
// class Solution {
// int[][] direction ={{1,0},{0,1},{-1,0},{0,-1}};
// public int numIslands(char[][] grid) {
// int ans=0;
// int m=grid.length,n=grid[0].length;
// boolean[][] visit=new boolean[m][n];
// Deque<int[]> dq = new ArrayDeque<>();
// for(int i=0;i<m;i++){
// for(int j=0;j<n;j++){
// if(grid[i][j]=='1'&&!visit[i][j]){
// dq.add(new int[]{i,j});
// visit[i][j]=true;
// while(!dq.isEmpty()){
// int[] node = dq.removeFirst();
// for(int k=0;k<4;k++){
// int nextx=node[0]+direction[k][0],nexty=node[1]+direction[k][1];
// if(0<=nextx&&nextx<m&&0<=nexty&&nexty<n&&grid[nextx][nexty]=='1'&&!visit[nextx][nexty]){
// dq.add(new int[]{nextx,nexty});
// visit[nextx][nexty]=true;
// }
// }
// }
// ans++;
// }
// }
// }
// return ans;
// }
// }
//广度优先遍历
//不需要额外的visit数组
//时间复杂度o(mn)
//空间复杂度o(m+n)
// class Solution {
// int[][] direction ={{1,0},{0,1},{-1,0},{0,-1}};
// public int numIslands(char[][] grid) {
// int ans=0;
// int m=grid.length,n=grid[0].length;
// Deque<int[]> dq = new ArrayDeque<>();
// for(int i=0;i<m;i++){
// for(int j=0;j<n;j++){
// if(grid[i][j]=='1'){
// dq.add(new int[]{i,j});
// grid[i][j]='0';
// while(!dq.isEmpty()){
// int[] node = dq.removeFirst();
// for(int k=0;k<4;k++){
// int nextx=node[0]+direction[k][0],nexty=node[1]+direction[k][1];
// if(0<=nextx&&nextx<m&&0<=nexty&&nexty<n&&grid[nextx][nexty]=='1'){
// dq.add(new int[]{nextx,nexty});
// grid[nextx][nexty]='0';
// }
// }
// }
// ans++;
// }
// }
// }
// return ans;
// }
// }
//深度优先搜索
//时间复杂度o(mn)
//空间复杂度o(mn)
// class Solution {
// int[][] direction ={{1,0},{0,1},{-1,0},{0,-1}};
// public int numIslands(char[][] grid) {
// int ans=0;
// int m=grid.length,n=grid[0].length;
// for(int i=0;i<m;i++){
// for(int j=0;j<n;j++){
// if(grid[i][j]=='1'){
// dfs(grid,i,j);
// ans++;
// }
// }
// }
// return ans;
// }
// private void dfs(char[][] grid,int r,int c){
// int m=grid.length,n=grid[0].length;
// grid[r][c]='0';
// for(int i=0;i<4;i++){
// int nextx=r+direction[i][0],nexty=c+direction[i][1];
// if(0<=nextx&&nextx<m&&0<=nexty&&nexty<n&&grid[nextx][nexty]=='1'){
// dfs(grid,nextx,nexty);
// }
// }
// }
// }
//并查集
// class Solution {
// class UnionFind{
// int cnt;
// int[] parent;
// public UnionFind(char[][] grid){
// int m=grid.length,n=grid[0].length;
// parent=new int[m*n];
// for(int i=0;i<m;i++){
// for(int j=0;j<n;j++){
// if(grid[i][j]=='1'){
// cnt++;
// parent[i*n+j]=i*n+j;
// }
// }
// }
// }
// public int find(int x){
// while(x!=parent[x]) x=parent[x];
// return x;
// }
// public void union(int x,int y){
// int rootx=find(x),rooty=find(y);
// if(rootx==rooty){
// return;
// }
// parent[rootx]=rooty;
// cnt--;
// }
// public int getCnt(){
// return cnt;
// }
// }
// int[][] direction ={{1,0},{0,1},{-1,0},{0,-1}};
// public int numIslands(char[][] grid) {
// if (grid == null || grid.length == 0) {
// return 0;
// }
// int m=grid.length,n=grid[0].length;
// UnionFind uf = new UnionFind(grid);
// for(int i=0;i<m;i++){
// for(int j=0;j<n;j++){
// if(grid[i][j]=='1'){
// grid[i][j]='0';
// for(int k=0;k<4;k++){
// int nextx=i+direction[k][0],nexty=j+direction[k][1];
// if(0<=nextx&&nextx<m&&0<=nexty&&nexty<n&&grid[nextx][nexty]=='1'){
// uf.union(i*n+j,nextx*n+nexty);
// }
// }
// }
// }
// }
// return uf.getCnt();
// }
// }
class Solution {
int[][] direction = {{1,0},{0,1},{-1,0},{0,-1}};
public int numIslands(char[][] grid) {
int m=grid.length,n=grid[0].length,ans=0;
boolean[][] visit = new boolean[m][n];
Deque<int[]> dq = new ArrayDeque<>();
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j]=='1'&&!visit[i][j]){
dq.add(new int[]{i,j});
visit[i][j]=true;
while(!dq.isEmpty()){
int[] pos = dq.removeFirst();
for(int k=0;k<4;k++){
int newx=pos[0]+direction[k][0];
int newy=pos[1]+direction[k][1];
if(0<=newx&&newx<m&&0<=newy&&newy<n&&grid[newx][newy]=='1'&&!visit[newx][newy]) {
dq.add(new int[]{newx,newy});
visit[newx][newy]=true;
}
}
}
ans++;
}
}
}
return ans;
}
}