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 ...
随机推荐
- emoji等表情符号存mysql的方法
项目中需要存储用户信息(用户昵称有表情符号),自然就遇到了emoji等表情符号如何被mysql DB支持的问题 这里引用先行者博文:https://segmentfault.com/a/1190000 ...
- PHP调用新浪API 生成短链接
我们经常收到类似于这样的短信(如下图),发现其中的链接并不是常规的网址链接,而是个短小精悍的短链接,产品中经常需要这样的需求,如果在给用户下发的短信中是一个很长的连接,用户体验肯定很差,因此我们需要实 ...
- simulation clock gen unit (推荐)
//Normal Clock Block always begin:clk_blk clk <=; # clk<=; #; end //Improved Clock Block, impr ...
- Django ORM (一) 创建数据库和模型常用的字段类型参数及Field 重要参数介绍
创建一个 Django 项目及应用 django-admin startproject orm cd orm python manage.py startapp app01 在 models.py 上 ...
- u-boot顶层Makefile分析
1.u-boot制作命令 make forlinx_nand_ram256_config: make all; 2.顶层mkconfig分析,参考 U-BOOT顶层目录mkconfig分析 mkcon ...
- Educational Codeforces Round 31- D. Boxes And Balls
D. Boxes And Balls time limit per test2 seconds memory limit per test256 megabytes 题目链接:http://codef ...
- luogu1963 [NOI2009]变换序列
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int ...
- HDU——3786找出直系亲属(DFS+回溯)
找出直系亲属 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- Caffe的Solver参数设置
Caffe的solver参数设置 http://caffe.berkeleyvision.org/tutorial/solver.html solver是通过协调前向-反向传播的参数更新来控制参数优化 ...
- Lucas定理详解
这篇博客是从另一位园友那里存的,但是当时忘了写原文的地址,如果有找到原文地址的请评论联系! Lucas定理解决的问题是组合数取模.数学上来说,就是求 \(\binom n m\mod p\).(p为素 ...