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 ...
随机推荐
- JavaScript正则表达式-边界符
^:表示字符串开始位置,在多行匹配中表示一行的开始位置. /^\w+/匹配字符串中第一个单词. $:表示字符串结束的位置,在多行匹配中表示一行的结束位置. /\w+$/匹配字符串中最后一个单词. /@ ...
- 安装mongodb卡顿
"3.6下载安装会卡死."的原因在于,默认安装是包含mongodb compass,这个包的,这个包大约有180MB,下载很慢,导致所谓的的”卡死“. 如果你选用自定义安装的并且不 ...
- 1 producer — n consumers 模型 实现
#include<stdio.h> #include<string.h> #include<pthread.h> #include<stdlib.h> ...
- 10大mysql需要注意的参数
MySQL变量很多,其中有一些MySQL变量非常值得我们注意,下面就为您介绍一些值得我们重点学习的MySQL变量,供您参考. 1 Threads_connected 首先需要注意的,想得到这个变量的值 ...
- 【RAID】raid1 raid2 raid5 raid6 raid10的优缺点和做各自raid需要几块硬盘
Raid 0:一块硬盘或者以上就可做raid0优势:数据读取写入最快,最大优势提高硬盘容量,比如3快80G的硬盘做raid0 可用总容量为240G.速度是一样.缺点:无冗余能力,一块硬盘损坏,数据全无 ...
- Tinkoff Challenge - Final Round (Codeforces Round #414, rated, Div. 1 + Div. 2) 继续跪一把
这次的前三题挺简单的,可是我做的不快也不对. A. Bank Robbery time limit per test 2 seconds memory limit per test 256 megab ...
- 83. Spring Boot 1.4单元测试【从零开始学Spring Boot】
在[27. Spring Boot Junit单元测试]中讲过1.3版本的单元测试方式,这里说说1.4和1.3有什么区别之处? 在1.3中单元测试这样子的类似代码: //// SpringJUnit支 ...
- Python之转换py文件为无需依赖python环境的exe文件的方法
在日常工作中,使用python脚本开发快速敏捷,但是其代码是可见的,而且充分的依赖python开发环境.为了达到保护我们源码的目的,或者不依赖python开发环境使用python脚本,将其转换成可以直 ...
- 算法复习——序列分治(ssoj光荣的梦想)
题目: 题目描述 Prince对他在这片大陆上维护的秩序感到满意,于是决定启程离开艾泽拉斯.在他动身之前,Prince决定赋予King_Bette最强大的能量以守护世界.保卫这里的平衡与和谐.在那个时 ...
- Django REST
一.什么是RESTful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移” REST从资源的角度 ...