• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅

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

posted @ 2015-04-11 05:20  neverlandly  阅读(2351)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3