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 operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each addLand operation. 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:
Given m = 3, n = 3
, positions = [[0,0], [0,1], [1,2], [2,1]]
.
Initially, the 2d grid grid
is filled with water. (Assume 0 represents water and 1 represents land).
0 0 0
0 0 0
0 0 0
Operation #1: addLand(0, 0) turns the water at grid[0][0] into a land.
1 0 0
0 0 0 Number of islands = 1
0 0 0
Operation #2: addLand(0, 1) turns the water at grid[0][1] into a land.
1 1 0
0 0 0 Number of islands = 1
0 0 0
Operation #3: addLand(1, 2) turns the water at grid[1][2] into a land.
1 1 0
0 0 1 Number of islands = 2
0 0 0
Operation #4: addLand(2, 1) turns the water at grid[2][1] into a land.
1 1 0
0 0 1 Number of islands = 3
0 1 0
We return the result as an array: [1, 1, 2, 3]
public List<Integer> numIslands2(int m, int n, int[][] positions) {
int[] rootArray = new int[m*n];
Arrays.fill(rootArray,-1); ArrayList<Integer> result = new ArrayList<Integer>(); int[][] directions = {{-1,0},{0,1},{1,0},{0,-1}};
int count=0; for(int k=0; k<positions.length; k++){
count++; int[] p = positions[k];
int index = p[0]*n+p[1];
rootArray[index]=index;//set root to be itself for each node for(int r=0;r<4;r++){
int i=p[0]+directions[r][0];
int j=p[1]+directions[r][1]; if(i>=0&&j>=0&&i<m&&j<n&&rootArray[i*n+j]!=-1){
//get neighbor's root
int thisRoot = getRoot(rootArray, i*n+j);
if(thisRoot!=index){
rootArray[thisRoot]=index;//set previous root's root
count--;
}
}
} result.add(count);
} return result;
} public int getRoot(int[] arr, int i){
while(i!=arr[i]){
i=arr[i];
}
return i;
}
二刷:
public class Solution {
public List<Integer> numIslands2(int m, int n, int[][] positions) {
int[] id = new int[m * n]; // 表示各个index对应的root List<Integer> res = new ArrayList<>();
Arrays.fill(id, -1); // 初始化root为-1,用来标记water, 非-1表示land
int count = 0; // 记录island的数量 int[][] dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
for (int i = 0; i < positions.length; i++) {
count++;
int index = positions[i][0] * n + positions[i][1];
id[index] = index; // root初始化 for (int j = 0; j < dirs.length; j++) {
int x = positions[i][0] + dirs[j][0];
int y = positions[i][1] + dirs[j][1];
if (x >= 0 && x < m && y >= 0 && y < n && id[x * n + y] != -1) {
int root = root(id, x * n + y); // 发现root不等的情况下,才union, 同时减小count
if (root != index) {
id[root] = index;
count--;
}
}
}
res.add(count);
}
return res;
} public int root(int[] id, int i) {
while (i != id[i]) {
id[i] = id[id[i]]; // 优化,为了减小树的高度
i = id[i];
}
return i;
}
}
LeetCode – Number of Islands II的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- [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 surro ...
- [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 ...
- LeetCode 305. Number of Islands II
原题链接在这里:https://leetcode.com/problems/number-of-islands-ii/ 题目: A 2d grid map of m rows and n column ...
- [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 ...
- 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 ...
- [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 ...
随机推荐
- Saiku二次开发获取源代码在本地编译(五)
关于Saiku的二次开发,在本地编译然后启动自己编译好的Saiku服务 Saiku是开源的,从github上能下载源代码,本例中的saiku源码也是从github上找的,然后自己改了一些pom.xml ...
- Cracking The Coding Interview 5.5
#include <iostream> #include <vector> using namespace std; int getNum1(int N) { int num= ...
- error MSB8008: 指定的平台工具集(V120)未安装或无效。
打开项目属性->配置属性->右面,平台工作集,选为v10 如下图
- Android : alsa-lib 移植
一.官网下载lib源码 网址:http://www.alsa-project.org/main/index.php/Download#alsa-lib 左击:Stable Release列表中的[1. ...
- 玩转X-CTR100 l STM32F4 l FPU单精度浮点性能测试
我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] 本文介绍X-CTR100控制器如何开启ST ...
- Java与C++简单对比
Java语言让编程者无法找到指针来直接访问内存,并且增添了自动的内存管理功能,从而有效的组织了C/C++语言中指针操作失误,如滥用指针所造成的系统崩溃,Java的指针在虚拟机内部使用,这保证了Java ...
- SharePoint Framework 企业向导(六)
博客地址:http://blog.csdn.net/FoxDave 接上一讲 部署SPFx解决方案 部署SPFx解决方案可以用两个步骤完成:1. 将脚本组件打成的包部署到一个CDN(内容分发网络) ...
- MySQL:存储过程和函数
存储过程和函数 一.创建存储过程和函数 1.创建存储过程 语法: CREATE PROCEDURE sp_name ([proc_parameter[,...]]) [characteristic . ...
- FCC JS基础算法题(6):Truncate a string(截断字符串)
先看一下题目描述: 如果字符串的长度比指定的参数num长,则把多余的部分用...来表示.切记,插入到字符串尾部的三个点号也会计入字符串的长度.但是,如果指定的参数num小于或等于3,则添加的三个点号不 ...
- 4--Selenium环境准备---chromedriver.exe 与chrome版本匹配
0.jdk8 和eclipse 4.6 https://www.eclipse.org/downloads/packages/release/neon/3 1.selenium-server-stan ...