原题链接在这里: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. Delphi TButton.OnClick 匿名函数用法

    type TNotifyEventRef = reference to procedure(Sender: TObject); function AnonymousEvent(const Proc: ...

  2. aop 打印请求信息

    项目中使用 AOP 打印请求信息,打印响应信息.package com.example.aspect; import com.alibaba.fastjson.JSON;import com.goog ...

  3. SpringBoot使用mybatis,发生:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured

    最近,配置项目,使用SpringBoot2.2.1,配置mybatis访问db,配好后,使用自定义的数据源.启动发生: APPLICATION FAILED TO START ************ ...

  4. JS把格林威治时间转换为北京标准时间

    function fermitTime(time){ var now = new Date(time); var year = now.getFullYear(); ; var date= now.g ...

  5. pandas-07 DataFrame修改index、columns名的方法

    pandas-07 DataFrame修改index.columns名的方法 一般常用的有两个方法: 1.使用DataFrame.index = [newName],DataFrame.columns ...

  6. EVM靶机渗透

    前言 靶机下载地址: https://download.vulnhub.com/norzhctf/Basilic.ova 通过渗透靶机来学习相关知识以便提高在真实环境的渗透能力 ==. 安装: 我是用 ...

  7. 介绍一个免费的云开发工具:Cloud Shell

    上周和一德国同事吹牛的时候,他说最近业余时间在玩一个东东,叫做Cloud Shell,Google出品.Jerry之前听说过国内的阿里云也提供过类似的解决方案,即在云端提供一个受限制的Linux环境并 ...

  8. Openlayers学习开始前序

    Openlayers将抽象事物具体化为类,其核心类是Map.Layers.Source.View,几乎所有的动作都是围绕这几个核心类展开.Map的实例就是内嵌于网页的交互式地图,因此,最关键的是Map ...

  9. Java读书笔记

    一.背景 工作中越来越发现对基础理解的重要性,所谓基础不牢,地动山摇.所以抽了点时间看看几本Java体系的电子书,并做笔记,如下: 二.近期要看的电子书 1.Spring实战(第4版) 2.Sprin ...

  10. 【TTS】传输表空间AIX asm -> linux asm

    [TTS]传输表空间AIX asm -> linux asm 一.1  BLOG文档结构图       一.2  前言部分   一.2.1  导读和注意事项 各位技术爱好者,看完本文后,你可以掌 ...