LeetCode 959. Regions Cut By Slashes
原题链接在这里:https://leetcode.com/problems/regions-cut-by-slashes/
题目:
In a N x N grid composed of 1 x 1 squares, each 1 x 1 square consists of a /, \, or blank space. These characters divide the square into contiguous regions.
(Note that backslash characters are escaped, so a \ is represented as "\\".)
Return the number of regions.
Example 1:
Input:
[
" /",
"/ "
]
Output: 2
Explanation: The 2x2 grid is as follows:
![]()
Example 2:
Input:
[
" /",
" "
]
Output: 1
Explanation: The 2x2 grid is as follows:
![]()
Example 3:
Input:
[
"\\/",
"/\\"
]
Output: 4
Explanation: (Recall that because \ characters are escaped, "\\/" refers to \/, and "/\\" refers to /\.)
The 2x2 grid is as follows:
![]()
Example 4:
Input:
[
"/\\",
"\\/"
]
Output: 5
Explanation: (Recall that because \ characters are escaped, "/\\" refers to /\, and "\\/" refers to \/.)
The 2x2 grid is as follows:
![]()
Example 5:
Input:
[
"//",
"/ "
]
Output: 3
Explanation: The 2x2 grid is as follows:
Note:
1 <= grid.length == grid[0].length <= 30grid[i][j]is either'/','\', or' '.
题解:
If one grid is divided by both '/' and '\\', then it could be 4 regions.
Mark them as 0,1,2,3 for top, right, bottom and left part.
If there is no '/', then 0 and 1 are unioned, 2 and 3 are unioned.
If there is no '\\', then 0 and 3 are unioned, 1 and2 are unioned.
For two adjacent grids, left grid part 1 and right grid part 3 are unioned. top grid part 2 and bottom grid part 0 are unioned.
Finally return unions count.
Time Complexity: O(n^2logn). n = grid.length. find takes O(log(n^2)) = O(logn). With path compression and union by weight, amatorize O(1).
Space: O(n^2).
AC Java:
class Solution {
int [] parent;
int [] size;
int count;
int n;
public int regionsBySlashes(String[] grid) {
if(grid == null || grid.length == 0){
return 0;
}
n = grid.length;
parent = new int[n*n*4];
size = new int[n*n*4];
count = n*n*4;
for(int i = 0; i<n*n*4; i++){
parent[i] = i;
size[i] = 1;
}
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
if(i > 0){
union(getIndex(i-1, j, 2), getIndex(i, j, 0));
}
if(j > 0){
union(getIndex(i, j-1, 1), getIndex(i, j, 3));
}
if(grid[i].charAt(j) != '/'){
union(getIndex(i, j, 0), getIndex(i, j, 1));
union(getIndex(i, j, 2), getIndex(i, j, 3));
}
if(grid[i].charAt(j) != '\\'){
union(getIndex(i, j, 0), getIndex(i, j, 3));
union(getIndex(i, j, 1), getIndex(i, j, 2));
}
}
}
return count;
}
private void union(int i, int j){
int p = find(i);
int q = find(j);
if(p != q){
if(size[p] > size[q]){
parent[q] = p;
size[p] += size[q];
}else{
parent[p] = q;
size[q] += size[p];
}
count--;
}
}
private int find(int i){
while(i != parent[i]){
parent[i] = parent[parent[i]];
i = parent[i];
}
return parent[i];
}
private int getIndex(int i, int j, int k){
return (i*n+j)*4+k;
}
}
LeetCode 959. Regions Cut By Slashes的更多相关文章
- LC 959. Regions Cut By Slashes
In a N x N grid composed of 1 x 1 squares, each 1 x 1 square consists of a /, \, or blank space. Th ...
- 【LeetCode】959. Regions Cut By Slashes 由斜杠划分区域(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 日期 题目地址:https://leetcod ...
- 【leetcode】959. Regions Cut By Slashes
题目如下: In a N x N grid composed of 1 x 1 squares, each 1 x 1 square consists of a /, \, or blank spac ...
- [Swift]LeetCode959. 由斜杠划分区域 | Regions Cut By Slashes
In a N x N grid composed of 1 x 1 squares, each 1 x 1 square consists of a /, \, or blank space. Th ...
- Leetcode959. Regions Cut By Slashes由斜杠划分区域
在由 1 x 1 方格组成的 N x N 网格 grid 中,每个 1 x 1 方块由 /.\ 或空格构成.这些字符会将方块划分为一些共边的区域. (请注意,反斜杠字符是转义的,因此 \ 用 &quo ...
- [LeetCode] Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 验证LeetCode Surrounded Regions 包围区域的DFS方法
在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...
- [leetcode]Surrounded Regions @ Python
原题地址:https://oj.leetcode.com/problems/surrounded-regions/ 题意: Given a 2D board containing 'X' and 'O ...
- Leetcode: Surrounded regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
随机推荐
- 【题解】Luogu P5279 [ZJOI2019]麻将
原题传送门 希望这题不会让你对麻将的热爱消失殆尽 我们珂以统计每种牌出现的次数,不需要统计是第几张牌 判一副牌能不能和,类似这道题 对于这题: 设\(f[i][j][k][0/1]\)表示前\(i\) ...
- 移动端开发rem布局之less+媒体查询布局的原理步骤和心得
rem即是以html文件中font-size的大小的倍数rem布局的原理:通过媒体查询设置不同屏幕宽度下的html的font-size大小,然后在css布局时用rem单位取代px,从而实现页面元素大小 ...
- yii框架中的各种小问题
1.默认路径的修改 2.分页的做法
- 详解java动态代理机制以及使用场景
详解java动态代理机制以及使用场景 https://blog.csdn.net/u011784767/article/details/78281384 深入理解java动态代理的实现机制 https ...
- 过滤器 & 监听器 & 拦截器
过滤器: https://blog.csdn.net/MissEel/article/details/79351231 https://blog.csdn.net/qq_32363305/articl ...
- netcore访问本地磁盘
public void ConfigureServices(IServiceCollection services) { services.AddDirectoryBrowser(); }public ...
- .Net Core WebApi(3)—NLog
在.Net Core中,微软提供的内置的日志组件没有实现将日志记录到文件.数据库上.这里使用NLog替代内置的日志组件 1.在项目中引入NuGet包 NLog NLog.Web.A ...
- AspNetCore MVC页面数据提交验证
2019/05/14,AspNetCore 2.2.0 摘要:AspNetCore MVC使用数据注释配合jquery.validate提交页面进行前端加后端的数据验证 主要用到了jquery.val ...
- 认识KNX协议
一.简介 KNX是Konnex的缩写.1999年5月,欧洲三大总线协议EIB.BatiBus和EHSA合并成立了Konnex协会,提出了KNX协议.该协议以EIB为基础,兼顾了BatiBus和EHSA ...
- Parameter 0 of method sqlSessionTemplate in org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration required a single bean, but 2 were found:
Parameter 0 of method orderSqlSessionFactory in com.config.MultipleDBConfig required a single bean, ...
