最后更新

一刷

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的更多相关文章

  1. 391 Perfect Rectangle 完美矩形

    有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域.每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1,2,2]. ( ...

  2. Leetcode: Perfect Rectangle

    Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...

  3. [LeetCode] Perfect Rectangle 完美矩形

    Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...

  4. [Swift]LeetCode391. 完美矩形 | Perfect Rectangle

    Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...

  5. Perfect Rectangle(完美矩形)

    我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域. 每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1,2,2] ...

  6. LeetCode赛题391----Perfect Rectangle

    #391. Perfect Rectangle Given N axis-aligned rectangles where N > 0, determine if they all togeth ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  9. LeetCode分类-前400题

    1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorte ...

随机推荐

  1. The Three Day

    函数基础-练习 #.写函数,,用户传入修改的文件名,与要修改的内容,执行函数,完成批了修改操作 # def modify_file(filename,old,new): # import os # w ...

  2. cvs 文件无法上传debug

    当时文件始终上传不成功时(一般先update后commit): cvs update filename report:move away filename ,it is in the way cvs ...

  3. $monitor用法

    1.$monitor 进程同一时间有且仅有一个,若多次调用$monitor,新进程会代替以前的monitor进程. 2.$fmonitor可以同时存在任意个. 3.一般不用$monitor系统函数. ...

  4. HTML 文件类表单元素如何限制上传类型,Accept属性设置

    需求描述:简单的控制file的选择类型 解决方法:使用HTML  input file 的accept属性控制 实例: <form action="demo_form.asp" ...

  5. errno的定义

    ./include/asm-generic/errno-base.h -->包含errno=~ ./arch/arm/include/asm/errno.h -->包含/include/a ...

  6. 关于在一台主机上安装2个不同版本的Oracle服务端

    一.安装Oracle12c 按正常安装方法安装即可! ORACLE_BASE=/u01/app ORACLE_HOME=/u01/app/oracle ORACLE_SID=a4orcl 二.安装Or ...

  7. selenium之定位以及切换frame

    总有人看不明白,以防万一,先在开头大写加粗说明一下: frameset不用切,frame需层层切! 很多人在用selenium定位页面元素的时候会遇到定位不到的问题,明明元素就在那儿,用firebug ...

  8. c#笔记2018-12-26

    using System; /*C#学习笔记2018-12-26 * 1.@逐字字符串 * 2.数据类型转换 * 3.变量声明和占位符使用 * 4.接收用户输入值 * 5.const 关键字 * 6. ...

  9. C++类指针初始化

    上面的代码会打印“A”. C++ 类指针定义的时候没有初始化的时候,居然可以安全的调用类内部的成员函数而不出错. 在网上查了一下:   初始化为NULL的类指针可以安全的调用不涉及类成员变量的类成员函 ...

  10. PHP 判断表单提交的file是否为空

    echo '<pre>'; var_dump($product_attach_files); echo "</pre>"; echo ];