# 1 解题思想

 核心思想就是:能够正好围成一个矩形的情况就是:

- 最左下 最左上 最右下 最右上 的四个点只出现过一次,其他肯定是成对出现的(保证完全覆盖)
- 上面四个点围成的面积,正好等于所有子矩形的面积之和(保证不重复)

# 2 原题

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

Each rectangle is represented as a bottom-left point and a top-right point. For example, a unit square is represented as [1,1,2,2]. (coordinate of bottom-left point is (1, 1) and top-right point is (2, 2)).

Example 1:

rectangles = [
[1,1,3,3],
[3,1,4,2],
[3,2,4,4],
[1,3,2,4],
[2,3,3,4]
]

Return true. All 5 rectangles together form an exact cover of a rectangular region.

Example 2:

rectangles = [
[1,1,2,3],
[1,3,2,4],
[3,1,4,2],
[3,2,4,4]
]

Return false. Because there is a gap between the two rectangular regions.

Example 3:

rectangles = [
[1,1,3,3],
[3,1,4,2],
[1,3,2,4],
[3,2,4,4]
]

Return false. Because there is a gap in the top center.

Example 4:

rectangles = [
[1,1,3,3],
[3,1,4,2],
[1,3,2,4],
[2,2,4,4]
]

Return false. Because two of the rectangles overlap with each other.


# 3 AC解

/**
* 核心思想就是:能够正好围成一个矩形的情况就是:
* 有且只有:
*   - 最左下 最左上 最右下 最右上 的四个点只出现过一次,其他肯定是成对出现的(保证完全覆盖)
*   - 上面四个点围成的面积,正好等于所有子矩形的面积之和(保证不重复)
* Created by MebiuW on 16/8/29.
*/
public class Solution {
public boolean isRectangleCover(int[][] rectangles) {
int left = Integer.MAX_VALUE;
int right = Integer.MIN_VALUE;
int top = Integer.MIN_VALUE;
int bottom = Integer.MAX_VALUE;
int n = rectangles.length;
HashSet<String> flags = new HashSet<String>();
int totalArea = 0;
for(int i=0;i<n;i++){
left = Math.min(left,rectangles[i][0]);
bottom = Math.min(bottom,rectangles[i][1]);
right = Math.max(right,rectangles[i][2]);
top = Math.max(top,rectangles[i][3]);
totalArea += (rectangles[i][3]-rectangles[i][1])*(rectangles[i][2]-rectangles[i][0]);
String pointLT = rectangles[i][0] + " "+ rectangles[i][3];
String pointLB = rectangles[i][0] + " "+ rectangles[i][1];
String pointRT = rectangles[i][2] + " "+ rectangles[i][3];
String pointRB = rectangles[i][2] + " "+ rectangles[i][1];
}
if(flags.size()==4 && flags.contains(left+" "+top) && flags.contains(left+" "+bottom) && flags.contains(right+" "+bottom) && flags.contains(right+" "+top)){
}
return false;
}
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：Leetcode 391. Perfect Rectangle 完美矩形 解题报告 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)