最后更新

一刷

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. 【http】【转发】HTTP访问控制(CORS)

    当一个资源从与该资源本身所在的服务器不同的域或端口请求一个资源时,资源会发起一个跨域 HTTP 请求.   比如,站点 http://domain-a.com 的某 HTML 页面通过 <img ...

  2. python index 自己实现

    l = [2,3,4,223,42,56,7,389,586,845,8,894,343,46,345,3556,23,233,45,25,78,456,785,576,344,6,34,563,] ...

  3. Python Jquery学习

    jquery调用方法: $(css的选择器).操作函数 语法格式: 操作函数: html      修改内容 点击button键后,jquery就会变为bootstrap 当然里面也可以进行判断,实现 ...

  4. BNUOJ 1055 走迷宫2

    走迷宫2 Time Limit: 1000ms Memory Limit: 65535KB   64-bit integer IO format: %lld      Java class name: ...

  5. PTA 10-排序4 统计工龄 (20分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/721 5-13 统计工龄   (20分) 给定公司NN名员工的工龄,要求按工龄增序输出每 ...

  6. NBOJv2——Problem 1002: 蛤玮的财宝(多线程DP)

    Problem 1002: 蛤玮的财宝 Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger IO format:  %ll ...

  7. [BZOJ4992] [Usaco2017 Feb]Why Did the Cow Cross the Road(spfa)

    传送门 把每个点和曼哈顿距离距离它3步或1步的点连一条边,边权为3 * t + a[x][y] 因为,走3步,有可能是3步,也有可能是1步(其中一步拐了回来) 最后,把终点和曼哈顿距离距离它1步和2布 ...

  8. Spring Open Session In View

    提出:session在应用层就关闭,所以持久化要在应用层,但是到了view层持久化则session已经关闭 解决:session延迟到view层再关闭 原理:session(整个requestScop ...

  9. #1045 - Access denied for user 'root'@'localhost' (using password: NO)的问题

    问题描述:  修改了root的密码,然后在http://localhost/phpmyadmin下无法登录了  报错:#1045 - Access denied for user 'root'@'lo ...

  10. 16.1116 NOIP 考前模拟(信心题)

    分火腿 (hdogs.pas/.c/.cpp) 时间限制:1s:内存限制 64MB 题目描述: 小月言要过四岁生日了,她的妈妈为她准备了n根火腿,她想将这些火腿均分给m位小朋友,所以她可能需要切火腿. ...