|
| 1 | +package code; |
| 2 | +/* |
| 3 | + * 200. Number of Islands |
| 4 | + * 题意:0,1矩阵,求1片区个数 |
| 5 | + * 难度:Medium |
| 6 | + * 分类:Depth-first Search, Breadth-first Search, Union Find |
| 7 | + * 思路:两种方法,一种搜索,一种并查集 |
| 8 | + * Tips: |
| 9 | + */ |
| 10 | +public class lc200 { |
| 11 | + public int numIslands(char[][] grid) { |
| 12 | + if(grid.length==0) |
| 13 | + return 0; |
| 14 | + int count = 0; |
| 15 | + for (int i = 0; i < grid.length ; i++) { |
| 16 | + for (int j = 0; j < grid[i].length ; j++) { |
| 17 | + if( grid[i][j]=='1' ){ |
| 18 | + count++; |
| 19 | + grid[i][j] = '0'; //置0, 下次不用考虑 |
| 20 | + search(grid,i,j); |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + return count; |
| 25 | + } |
| 26 | + public void search(char[][] grid, int i, int j){ |
| 27 | + if( i>0 && grid[i-1][j]=='1') { |
| 28 | + grid[i-1][j] = '0'; //置0, 下次不用考虑 |
| 29 | + search(grid, i - 1, j); |
| 30 | + } |
| 31 | + if( j>0 && grid[i][j-1]=='1') { |
| 32 | + grid[i][j-1] = '0'; |
| 33 | + search(grid, i, j-1); |
| 34 | + } |
| 35 | + if( i+1<grid.length && grid[i+1][j]=='1') { |
| 36 | + grid[i+1][j] = '0'; |
| 37 | + search(grid, i + 1, j); |
| 38 | + } |
| 39 | + if( j+1<grid[0].length && grid[i][j+1]=='1') { |
| 40 | + grid[i][j+1] = '0'; |
| 41 | + search(grid, i, j+1); |
| 42 | + } |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + //并查集 |
| 47 | + int[] flag; |
| 48 | + int count = 0; |
| 49 | + |
| 50 | + public int numIslands2(char[][] grid) { |
| 51 | + if(grid.length==0) |
| 52 | + return 0; |
| 53 | + //初始化, 每个1都是一个island |
| 54 | + flag = new int[grid.length*grid[0].length]; |
| 55 | + for (int i = 0; i < grid.length ; i++) { |
| 56 | + for (int j = 0; j < grid[0].length ; j++) { |
| 57 | + if(grid[i][j]=='1'){ |
| 58 | + int id = i*grid[0].length+j; |
| 59 | + flag[id] = id; |
| 60 | + count++; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + for (int i = 0; i < grid.length ; i++) { |
| 65 | + for (int j = 0; j < grid[0].length ; j++) { |
| 66 | + if(grid[i][j]=='1'){ |
| 67 | + int id1 = i*grid[0].length+j; |
| 68 | + if( i>0 && grid[i-1][j]=='1') { //四个方向都合一下,虽然有些冗余 |
| 69 | + int id2 = (i-1)*grid[0].length+j; |
| 70 | + union(id1, id2); |
| 71 | + } |
| 72 | + if( j>0 && grid[i][j-1]=='1') { |
| 73 | + int id2 = i*grid[0].length+j-1; |
| 74 | + union(id1, id2); |
| 75 | + } |
| 76 | + if( i+1<grid.length && grid[i+1][j]=='1') { |
| 77 | + int id2 = (i+1)*grid[0].length+j; |
| 78 | + union(id1, id2); |
| 79 | + } |
| 80 | + if( j+1<grid[0].length && grid[i][j+1]=='1') { |
| 81 | + int id2 = i*grid[0].length+j+1; |
| 82 | + union(id1, id2); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + return count; |
| 88 | + } |
| 89 | + |
| 90 | + public void union(int id1, int id2){ |
| 91 | + int flag_id1 = find(id1); |
| 92 | + int flag_id2 = find(id2); |
| 93 | + if(flag_id1==flag_id2) |
| 94 | + return; |
| 95 | + else{ |
| 96 | + flag[flag_id1] = flag[flag_id2]; //把father们的 flag_id 合一起了 |
| 97 | + count--; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public int find(int id){ //找到father的flag_id |
| 102 | + int res = flag[id]; |
| 103 | + while(res != id){ //接着往上找father |
| 104 | + id = res; |
| 105 | + res = flag[id]; |
| 106 | + } |
| 107 | + return res; |
| 108 | + } |
| 109 | +} |
0 commit comments