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
1 class Solution {//dfs my 2 public int numIslands(char[][] grid) { 3 if(null==grid||0==grid.length){ 4 return 0; 5 } 6 int row =grid.length; 7 int col = grid[0].length; 8 int re = 0; 9 boolean[][] visited=new boolean[row][col];//判断的是否访问过 10 for(int i=0;i<row;i++){ 11 for(int j=0;j<col;j++){ 12 if('1'==grid[i][j]&&!visited[i][j]){//为1且没有访问过,表示是另一个岛 13 dfs(grid,visited,i,j); 14 re++; 15 } 16 } 17 } 18 return re; 19 } 20 private void dfs(char[][] grid,boolean[][] visited,int i,int j){ 21 if(i<0||j<0||i>=grid.length||j>=grid[0].length){ 22 return; 23 } 24 if('1'==grid[i][j]&&!visited[i][j]){ 25 visited[i][j]=true; 26 dfs(grid,visited,i+1,j); 27 dfs(grid,visited,i-1,j); 28 dfs(grid,visited,i,j+1); 29 dfs(grid,visited,i,j-1); 30 } 31 } 32 }
使用并查集,且并查集合并使用rank(按秩合并即按高度)的方法
1 class Solution {//并查集 mytip 2 public int numIslands(char[][] grid) { 3 if(null==grid||0==grid.length){ 4 return 0; 5 } 6 int row =grid.length; 7 int col = grid[0].length; 8 int re = 0; 9 UnionFind uf = new UnionFind(grid); 10 int[][] dir ={{0,-1},{0,1},{-1,0},{1,0}};//方向数组 11 for(int i=0;i<row;i++){ 12 for(int j=0;j<col;j++){ 13 if('1'==grid[i][j]){ 14 for(int k=0;k<dir.length;k++){ 15 int x = i+dir[k][0]; 16 int y = j+dir[k][1]; 17 if(x>=0&&x<row&&y>=0&&y<col&&'1'==grid[x][y]){ 18 uf.union(i*col+j,x*col+y); 19 } 20 } 21 } 22 } 23 } 24 return uf.getCount(); 25 } 26 } 27 class UnionFind{ 28 private int[] parents;//各点的父节点 29 private int[] ranks;//各点的高 30 private int count;//本题的结果,即集合个数 31 public UnionFind(int n){ 32 parents= new int[n]; 33 ranks = new int[n]; 34 for(int i=0;i<n;i++){ 35 parents[i]=i; 36 } 37 } 38 public UnionFind(char[][] grid){ 39 int num = grid.length*grid[0].length; 40 parents = new int[num]; 41 ranks = new int[num]; 42 for(int i=0;i<grid.length;i++){ 43 for(int j=0;j<grid[0].length;j++){ 44 if('1'==grid[i][j]){ 45 parents[i*grid[0].length+j]= i*grid[0].length+j; 46 count++; 47 } 48 } 49 } 50 } 51 public int getCount(){ 52 return this.count; 53 } 54 public int find(int x){ 55 while(x!=parents[x]){ 56 x= parents[x]; 57 } 58 return x; 59 } 60 public void union(int x,int y){//分别找到各自的根节点,若根不相同则执行合并 61 int rootx = find(x); 62 int rooty = find(y); 63 if(rootx!=rooty){ 64 if(ranks[rootx]<ranks[rooty]){ 65 parents[rootx] = rooty; 66 } 67 else if(ranks[rooty]<ranks[rootx]){ 68 parents[rooty] = rootx; 69 } 70 else{ 71 parents[rooty]=rootx; 72 ranks[rootx]++; 73 } 74 count--; 75 } 76 77 } 78 }
适用并查集,且并查集合并适用路径压缩的方法
1 class Solution {//并查集 mytip 2 public int numIslands(char[][] grid) { 3 if(null==grid||0==grid.length){ 4 return 0; 5 } 6 int row =grid.length; 7 int col = grid[0].length; 8 int re = 0; 9 UnionFind uf = new UnionFind(grid); 10 int[][] dir ={{0,-1},{0,1},{-1,0},{1,0}}; 11 for(int i=0;i<row;i++){ 12 for(int j=0;j<col;j++){ 13 if('1'==grid[i][j]){ 14 for(int k=0;k<dir.length;k++){ 15 int x = i+dir[k][0]; 16 int y = j+dir[k][1]; 17 if(x>=0&&x<row&&y>=0&&y<col&&'1'==grid[x][y]){ 18 uf.union(i*col+j,x*col+y); 19 } 20 } 21 } 22 } 23 } 24 return uf.getCount(); 25 } 26 } 27 class UnionFind{ 28 private int[] parents;//根节点 29 private int count; 30 public UnionFind(int n){ 31 parents= new int[n]; 32 for(int i=0;i<n;i++){ 33 parents[i]=i; 34 } 35 } 36 public UnionFind(char[][] grid){ 37 parents = new int[grid.length*grid[0].length]; 38 for(int i=0;i<grid.length;i++){ 39 for(int j=0;j<grid[0].length;j++){ 40 if('1'==grid[i][j]){ 41 parents[i*grid[0].length+j]= i*grid[0].length+j; 42 count++; 43 } 44 } 45 } 46 } 47 public int getCount(){ 48 return this.count; 49 } 50 public int find(int x){//找到根节点,并把路径中所有点的父节点都设为根节点 51 int root = x; 52 while(root!=parents[root]){ 53 root= parents[root]; 54 } 55 while(parents[x]!=root){ 56 int tmp = parents[x]; 57 parents[x] =root; 58 x = tmp; 59 } 60 return root; 61 } 62 public void union(int x,int y){ 63 int rootx = find(x); 64 int rooty = find(y); 65 if(rootx!=rooty){ 66 parents[rooty] = rootx; 67 count--; 68 } 69 70 } 71 }