391. Perfect Rectangle
最后更新
一刷
16-Jan-2017
这个题我甚至不知道该怎么总结。
难就难在从这个题抽象出一种解法,看了别人的答案和思路= =然而没有归类总结到某种类型,这题相当于背了个题。。。
简单的说,除了最外面的4个点,所有的点都会2次2次的出现,如果有覆盖,覆盖进去的点就不是成对出现了。
最外面4个点围的面积等于所有小矩形面积的和。
就用这2个判断就行了。
判断成对的点用的SET,单次出现添加,第二次出现删除。。这样最后应该都删掉,SET里只剩下4个最外面的点。
剩下的就是判断最外点,不停地更新。。
public class Solution {
public boolean isRectangleCover(int[][] rectangles) {
if (rectangles.length == 0) return true;
int left = Integer.MAX_VALUE;
int bot = Integer.MAX_VALUE;
int right = Integer.MIN_VALUE;
int top = Integer.MIN_VALUE;
Set<Integer> set = new HashSet<>();
int tempArea = 0;
for (int[] nums : rectangles) {
// bot left top right
bot = Math.min(bot, nums[0]);
left = Math.min(left, nums[1]);
top = Math.max(top, nums[2]);
right = Math.max(right, nums[3]);
tempArea += (nums[2] - nums[0]) * (nums[3] - nums[1]);
int bottomLeft = 10 * nums[0] + nums[1];
int topRight = 10 * nums[2] + nums[3];
int bottomRight = 10 * nums[0] + nums[3];
int topLeft = 10 * nums[2] + nums[1];
if (set.contains(bottomLeft)) set.remove(bottomLeft);
else set.add(bottomLeft);
if (set.contains(topRight)) set.remove(topRight);
else set.add(topRight);
if (set.contains(bottomRight)) set.remove(bottomRight);
else set.add(bottomRight);
if (set.contains(topLeft)) set.remove(topLeft);
else set.add(topLeft);
}
int bottomLeft = 10 * bot + left;
int topRight = 10 * top + right;
int bottomRight = 10 * bot + right;
int topLeft = 10 * top + left;
int maxArea = (right - left) * (top - bot);
if(set.size() == 4 && set.contains(bottomLeft) && set.contains(topRight)
&& set.contains(bottomRight) && set.contains(topLeft)) {
return maxArea == tempArea;
} else {
return false;
}
}
}
391. Perfect Rectangle的更多相关文章
- 391 Perfect Rectangle 完美矩形
有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域.每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1,2,2]. ( ...
- Leetcode: Perfect Rectangle
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
- [LeetCode] Perfect Rectangle 完美矩形
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
- [Swift]LeetCode391. 完美矩形 | Perfect Rectangle
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
- Perfect Rectangle(完美矩形)
我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域. 每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1,2,2] ...
- LeetCode赛题391----Perfect Rectangle
#391. Perfect Rectangle Given N axis-aligned rectangles where N > 0, determine if they all togeth ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- LeetCode分类-前400题
1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorte ...
随机推荐
- solr 单机模式搭建
系统环境:centos 7 安装前准备 安装JDK环境 下载tomcat.solr安装包:solr下载地址:http://archive.apache.org/dist/lucene/solr/ 安装 ...
- 【Linux】启动Tomcat遇到Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
找不到JAVA_HOME路径,需要做以下变更: 找到启动路径所在的目录: cd bin/ vi catalina.sh 加入以下信息: export JAVA_HOME=/home/gongzi/ht ...
- 二段式fsm
1.推荐在敏感表下的默认状态为X,这样描述的好处有2个: 好处1:仿真易观察bug. 好处2:综合对不定态X的处理是"Don't Care",即任何没有定义的状态寄存器向量都会被忽 ...
- perl学习之裸字
use strict包含3个部分.其中之一(use strict "subs")负责禁止乱用的裸字. 这是什么意思呢? 如果没有这个限制,下面的代码也可以打印出"hell ...
- S3C6410串口平台设备注册流程分析
1.mdesc->map_io() start_kernel -->setup_arch(&command_line); -->paging_init(mdesc); --& ...
- scheduleWithFixedDelay和scheduleAtFixedRate源码分析
先放张图,有兴趣的话我再加细节说明. scheduleWithFixedDelay和scheduleAtFixedRate的执行流程都是一样的,如下 ScheduledThreadPoolExecut ...
- python 中zip()函数的使用
zip(*iterables)函数的定义: zip()函数的对象Iterables,iterables可以有多个参数(元组,列表等可迭代对象)组成.通过zip()函数返回一组元组数据,每个元组中的第i ...
- Codeforces 545E. Paths and Trees[最短路+贪心]
[题目大意] 题目将从某点出发的所有最短路方案中,选择边权和最小的最短路方案,称为最短生成树. 题目要求一颗最短生成树,输出总边权和与选取边的编号.[题意分析] 比如下面的数据: 5 5 1 2 2 ...
- 树形DP专题
DP是我的弱项, 此专题意在总结树形DP的解题思路. 最小代价遍历一棵树 给定一棵带边权的树 $T=(V,E)$ , 遍历它 (树的每个节点都访问至少一次) 所需的最小代价. 这里的代价由具体问题所定 ...
- hdu 1390
#include<stdio.h> int main() { int t,n,a[1001]; scanf("%d",&t); while(t--) { sca ...