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. Python3之字符串格式化format函数详解(上)

    概述 在Python3中,字符串格式化操作通过format()方法或者f’string’实现.而相比于老版的字符串格式化方式,format()方法拥有更多的功能,操作起来更加方便,可读性也更强.该函数 ...

  2. (三)shiro的认证

    文章目录 认证思路 自定义用于登录检验的Realm的思路 代码实现 后记 认证思路 调用 SecurityUtils.getSubject() 方法,获取当前的 Subject 对象 : 调用 Sub ...

  3. C++ 中 static 与 const 的用法及对比

    在这个学习过程中我对 static 及 const 的使用时常会混淆,因此整理,加深记忆 一.类的静态成员 如果某个属性为整个类所共有,不属于任何一个具体对象,则采用 static 关键字来声明静态成 ...

  4. python学习-57 logging模块

    logging 1.basicConfig方式 import logging # 以下是日志的级别 logging.debug('debug message') logging.info('info ...

  5. 第五章 模块之 struct、dis、正则表达式、异常处理

    5.15 struct模块 pack 能够把所有的数字都固定的转换成4字节 5.16 dis dis.dis 查看计算机指令 5.16 正则表达式 基础 正则表达式概念: 是一种规则(元字符,量词) ...

  6. AVR单片机教程——点亮第一个LED

    做了这么多准备,我们终于可以开始用开发板做点事了. 单片机编程与计算机编程有一些不同点.程序都要有零个或多个输入.一个或多个输出,这是两者都有的,但是计算机编程的输入输出主要靠控制台,而单片机没有. ...

  7. 【Linux】一步一步学Linux——Linux版本(03)

    目录 00. 目录 01. Linux内核版本 02. Linux内核官方网站 03. Linux发行版本 04. Linux发行版本介绍 4.1 Ubuntu 4.2 RedHat 4.3 Debi ...

  8. C/C++ cmake example

    学习 Golang,有时需要 Cgo,所以需要学习 C.C++. 语言入门: https://item.jd.com/12580612.html https://item.jd.com/2832653 ...

  9. lambda的一些用法

    lambda在函数中调用时可以不用传入形参,当需要时才传入参数,方便一些场合中的使用(当参数一直变化时,仍然需要调用函数,可以采用如下方式).如以下代码所示. import numpy as np d ...

  10. 关键字:__thread & pthread_key_t

    在说__thread之前,先来看看pthread_ket_t吧. 参考:http://blog.csdn.net/lmh12506/article/details/8452700 上面的博文说的比较通 ...