Given a n,m which means the row and column of the 2D matrix and an array of pair A( size k). Originally, the 2D matrix is all 0 which means there is only sea in the matrix. The list pair has k operator and each operator has two integer A[i].x, A[i].y means that you can change the grid matrix[A[i].x][A[i].y] from sea to island. Return how many island are there in the matrix after each operator.

0 is represented as the sea, 1 is represented as the island. If two 1 is adjacent, we consider them in the same island. We only consider up/down/left/right adjacent.

Example

Example 1:

Input: n = 4, m = 5, A = [[1,1],[0,1],[3,3],[3,4]]
Output: [1,1,2,2]
Explanation:
0. 00000
00000
00000
00000
1. 00000
01000
00000
00000
2. 01000
01000
00000
00000
3. 01000
01000
00000
00010
4. 01000
01000
00000
00011

Example 2:

Input: n = 3, m = 3, A = [[0,0],[0,1],[2,2],[2,1]]
Output: [1,1,2,2]
思路使用并查集,并查集的实现可以利用Point结构体, 也可以将二维坐标转换成一维。注意使用类的时候需重写equals方法和hashCode方法。

  对于每一次操作(x, y), 如果(x, y)的上下左右都是0, 那么计数器加一; 如果不全为0, 则:

  1. 并查集查询其四周的1所属的集合, 假设它们属于 k 个不同的集合
  2. 计数器减去 k-1
  3. 将这 k 个集合, 连同 (x, y), 合并
/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/ public class Solution {
/**
* @param n: An integer
* @param m: An integer
* @param operators: an array of point
* @return: an integer array
*/ class MyPoint{
int x;
int y;
MyPoint() { x = 0; y = 0; }
MyPoint(int a, int b) { x = a; y = b; } @Override
public boolean equals(Object obj) {
if (obj == this)
return true;
return this.x == ((MyPoint) obj).x && this.y == ((MyPoint) obj).y;
} @Override
public int hashCode() {
return x * 10 + y;
}
}
class UnionFind{
HashMap<MyPoint, MyPoint> father = new HashMap<>();
UnionFind(int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
MyPoint p = new MyPoint(i, j);
father.put(p, p);
}
}
} MyPoint find(MyPoint p) {
MyPoint point = p;
while (point != father.get(point)) {
point = father.get(point);
}
MyPoint root = point;
while (p != father.get(p)) {
MyPoint tmp = father.get(p);
father.put(tmp, root);
p = tmp;
}
return root;
} void union(MyPoint p1, MyPoint p2) {
MyPoint root1 = find(p1);
MyPoint root2 = find(p2);
if (root1 != root2) {
father.put(root1, root2);
}
}
}
public List<Integer> numIslands2(int n, int m, Point[] operators) {
List<Integer> ans = new ArrayList<>();
if (operators == null || operators.length == 0) {
return ans;
}
int[] dx = {0, -1, 0, 1};
int[] dy = {1, 0, -1, 0};
int[][] island = new int[n][m];
UnionFind uf = new UnionFind(n, m);
int count = 0;
for (int i = 0; i < operators.length; i++) {
int x = operators[i].x;
int y = operators[i].y;
if (island[x][y] != 1) {
island[x][y] = 1;
count++;
MyPoint p = new MyPoint(x, y);
for (int j = 0; j < 4; j++) {
int nx = x + dx[j];
int ny = y + dy[j];
if (0 <= nx && nx < n && 0 <= ny && ny < m && island[nx][ny] == 1) {
MyPoint np = new MyPoint(nx, ny);
MyPoint root1 = uf.find(p);
MyPoint root2 = uf.find(np);
if (root1 != root2) {
count--;
uf.union(p, np);
}
}
}
}
ans.add(count);
}
return ans;
}
}

  

Number of Islands II的更多相关文章

  1. [LeetCode] Number of Islands II 岛屿的数量之二

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  2. [LeetCode] 305. Number of Islands II 岛屿的数量之二

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  3. [LeetCode] Number of Islands II

    Problem Description: A 2d grid map of m rows and n columns is initially filled with water. We may pe ...

  4. Leetcode: Number of Islands II && Summary of Union Find

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  5. 305. Number of Islands II

    题目: A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand  ...

  6. [Swift]LeetCode305. 岛屿的个数 II $ Number of Islands II

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  7. LeetCode – Number of Islands II

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  8. LeetCode 305. Number of Islands II

    原题链接在这里:https://leetcode.com/problems/number-of-islands-ii/ 题目: A 2d grid map of m rows and n column ...

  9. [LeetCode] 305. Number of Islands II 岛屿的数量 II

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  10. LintCode "Number of Islands II"

    A typical Union-Find one. I'm using a kinda Union-Find solution here. Some boiler-plate code - yeah ...

随机推荐

  1. LeetCode 637. 二叉树的层平均值(Average of Levels in Binary Tree)

    637. 二叉树的层平均值 637. Average of Levels in Binary Tree LeetCode637. Average of Levels in Binary Tree 题目 ...

  2. 《Mysql - 如何恢复和避免误删除?》

    一:误删数据 (如何恢复和避免误删除) - 使用 delete 语句误删数据行: - 使用 drop table 或者 truncate table 语句误删数据表: - 使用 drop databa ...

  3. ajax后台跳转无效的原因

    Ajax只是利用脚本访问对应url获取数据而已,不能做除了获取返回数据以外的其它动作了.所以浏览器端是不会发起重定向的. 1)正常的http url请求,只有浏览器和服务器两个参与者.浏览器端发起一个 ...

  4. Hystrix【入门】

    公共依赖配置: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spr ...

  5. 二进制知识(java中的位操作)

    文章目录 前言 机器数 真值 原码 反码 补码 计算机中保存的都是补码 位操作 强制转换,精度丢失 前言 讲二进制的东西,必须要说明是多少位机器,八位机上的 1000 1000 和 十六位机上的 10 ...

  6. Python Http-server 使用

    Python内置的下载服务器 http.server  Python的Web服务器 python2 中SimpleHTTPServer python3 中 http.server 执行 python ...

  7. 20 闭包、nonlocal

    闭包的概念 闭包就是能够读取其他函数内部变量的函数. 从模块级别调用函数内部的局部变量. 闭包 = 函数+环境变量(函数外部的变量) 闭包存在的条件 闭包必须返回一个函数 被返回的函数必须调用环境变量 ...

  8. Docker 方式部署的应用的版本更新

    前言 公司使用 Docker-Compose 的方式部署 Jenkins/Gitlab/Sonar/Confluence/Apollo/Harbor/ELK/MySQL 等一系列开发工具/数据库. 而 ...

  9. C#委托,匿名方法,Lambda,泛型委托,表达式树代码示例

    第一分钟:委托 有些教材,博客说到委托都会提到事件,虽然事件是委托的一个实例,但是为了理解起来更简单,今天只谈委托不谈事件.先上一段代码: 下边的代码,完成了一个委托应用的演示.一个委托分三个步骤: ...

  10. redis设置密码,解决重启后密码丢失及自启服务配置

    一.安装redis redis3.0及redisManage管理工具 链接:https://pan.baidu.com/s/1p5EWeF2Jgsw9xOE1ADMmRg 提取码:thyf 二.red ...