原题链接在这里: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. 1 <= grid.length == grid[0].length <= 30
  2. grid[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的更多相关文章

  1. 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 ...

  2. 【LeetCode】959. Regions Cut By Slashes 由斜杠划分区域(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 日期 题目地址:https://leetcod ...

  3. 【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 ...

  4. [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 ...

  5. Leetcode959. Regions Cut By Slashes由斜杠划分区域

    在由 1 x 1 方格组成的 N x N 网格 grid 中,每个 1 x 1 方块由 /.\ 或空格构成.这些字符会将方块划分为一些共边的区域. (请注意,反斜杠字符是转义的,因此 \ 用 &quo ...

  6. [LeetCode] Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  7. 验证LeetCode Surrounded Regions 包围区域的DFS方法

    在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...

  8. [leetcode]Surrounded Regions @ Python

    原题地址:https://oj.leetcode.com/problems/surrounded-regions/ 题意: Given a 2D board containing 'X' and 'O ...

  9. Leetcode: Surrounded regions

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

随机推荐

  1. [NOI2019]弹跳(KD-Tree/四分树/线段树套平衡树 优化建图+Dijkstra)

    本题可以用的方法很多,除去以下三种我所知道的就还有至少三种. 方法一:类似线段树优化建图,将一个平面等分成四份(若只有一行或一列则等分成两份),然后跑Dijkstra即可.建树是$O(n\log n) ...

  2. MyBatis返回结果类型为Boolean

    问题描述:        在使用MyBatis时,有时需要检查某个记录是否存在数据库中,然后根据其返回的布尔值true or false,来进行逻辑判断.那怎么做呢? 解决方案: 如检测某个手机号是否 ...

  3. 关于django数据库迁移 以及显示未检测到更改的问题

    No changes detected 显示这样的原因 数据库迁移代码步骤: 今天在所有数据库的时候对数据库进行了删除,重新迁移数据库映射,但是却发现终端给出了这样的信息. '>>> ...

  4. git恢复已删的分支

    git恢复已经删除的分支 执行git命令, 找回之前提交的commit git log -g 执行效果 commit 80fd3a3e1abeab52030ee9f6ec32b5c815de20a9 ...

  5. Django使用 django-allauth实现第三方登陆

    Django使用 django-allauth实现第三方登陆 这里我们使用 django-allauth 模块来实现第三方账号验证登录,官方文档如下:https://django-allauth.re ...

  6. python day 20: 线程池与协程,多进程TCP服务器

    目录 python day 20: 线程池与协程 2. 线程 3. 进程 4. 协程:gevent模块,又叫微线程 5. 扩展 6. 自定义线程池 7. 实现多进程TCP服务器 8. 实现多线程TCP ...

  7. vue动态循环出的多个select出现过的变为disabled

    <template> <div class="artcle"> <el-form label-width="100px" :mod ...

  8. Flask 和 Django 框架的区别

    1)Flask Flask确实很“轻”,不愧是Micro Framework,从Django转向Flask的开发者一定会如此感慨,除非二者均为深入使用过 Flask自由.灵活,可扩展性强,第三方库的选 ...

  9. 英语wacche腕表

    手表 (戴在手腕上的计时仪器) 手表,或称为腕表,是指戴在手腕上,用以计时/显示时间的仪器,手表在英语里watch源自中世纪wacche这一词汇. 手表通常是利用皮革.橡胶.尼龙布.不锈钢等材料,制成 ...

  10. ubuntu16.04安装openssh中报错解决

    在使用apt-get直接进行安装时会报错: sudo apt-get install openssh-server 正在读取软件包列表... 完成 正在分析软件包的依赖关系树       正在读取状态 ...