Leetcode: Number of Islands


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:

11110
11010
11000
00000
Answer: 1

Example 2:

11000
11000
00100
00011
Answer: 3

DFS的Flood Fill方法,

不使用額外visited數組,但是用‘1’變成‘2’表示visited的方法

復雜度

時間 O(NM) 空間 O(max(N,M)) 遞歸棧空間

 1 public class Solution {
 2     public int numIslands(char[][] grid) {
 3         if (grid==null || grid.length==0 || grid[0].length==0) return 0;
 4         int count = 0;
 5         for (int i=0; i<grid.length; i++) {
 6             for (int j=0; j<grid[0].length; j++) {
 7                 if (grid[i][j] != '1') continue;
 8                 else {
 9                     count++;
10                     floodFill(grid, i, j);
11                 }
12             }
13         }
14         return count;
15     }
16     
17     public void floodFill(char[][] grid, int i, int j) {
18         if (i<0 || i>=grid.length || j<0 || j>=grid[0].length) return;
19         if (grid[i][j] != '1') return; //either 0(water) or 2(visited)
20         grid[i][j] = '2';
21         floodFill(grid, i-1, j);
22         floodFill(grid, i+1, j);
23         floodFill(grid, i, j-1);
24         floodFill(grid, i, j+1);
25     }
26 }

 

Union Find: (Quick Union)

 1 class Solution {
 2     public int numIslands(char[][] grid) {
 3         if (grid == null || grid.length == 0 || grid[0].length == 0) return 0;
 4         int m = grid.length;
 5         int n = grid[0].length;
 6         unionFind uf = new unionFind(m * n);
 7         for (int i = 0; i < m; i ++) {
 8             for (int j = 0; j < n; j ++) {
 9                 if (grid[i][j] == '0') continue;
10                 uf.fathers[i * n + j] = i * n + j;
11                 uf.count ++;
12                 if (i > 0 && grid[i - 1][j] == '1') uf.union(i * n + j, (i - 1) * n + j);
13                 if (j > 0 && grid[i][j - 1] == '1') uf.union(i * n + j, i * n + j - 1);
14             }
15         }
16         return uf.count;
17     }
18     
19     class unionFind {
20         int[] fathers;
21         int count;
22         public unionFind(int n) {
23             this.fathers = new int[n];
24             Arrays.fill(fathers, -1);
25             this.count = 0;
26         }
27         
28         public void union(int i, int j) {
29             if (isConnected(i, j)) return; 
30             int iRoot = find(i);
31             int jRoot = find(j);
32             fathers[iRoot] = jRoot;
33             count --;
34         }
35         
36         public int find(int i) {
37             while (fathers[i] != i) {
38                 i = fathers[i];
39             }
40             return i;
41         }
42         
43         public boolean isConnected(int i, int j) {
44             return find(i) == find(j);
45         }
46     }
47 }

 

 

 

follow up是找最大島的面積,想法是設置兩個全局變量 maxArea, area, area負責記錄每個島的面積,上面代碼第8行每次遇到‘1’就置為1,然后在recursion里面++,用area的最大值更新maxArea

 

follow up是數unique island, 比如

110000

110001

001101

101100

100000

總共兩個unique島,不是四個

方法可以是記錄每次新的島嶼搜索的路徑,left,right,up,down, 作為標志是否相同的key,存hashset

code 參 FB面經 Prepare: Count Unique Islands


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM