|
3 | 3 | importjava.util.HashSet;
|
4 | 4 | importjava.util.Set;
|
5 | 5 |
|
6 |
| -/** |
7 |
| - * 391. Perfect Rectangle |
8 |
| - * |
9 |
| - * Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover of a rectangular region. |
10 |
| - Each rectangle is represented as a bottom-left point and a top-right point. |
11 |
| - 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)). |
12 |
| -
|
13 |
| - Example 1: |
14 |
| -
|
15 |
| - rectangles = [ |
16 |
| - [1,1,3,3], |
17 |
| - [3,1,4,2], |
18 |
| - [3,2,4,4], |
19 |
| - [1,3,2,4], |
20 |
| - [2,3,3,4] |
21 |
| - ] |
22 |
| -
|
23 |
| - Return true. All 5 rectangles together form an exact cover of a rectangular region. |
24 |
| -
|
25 |
| - Example 2: |
26 |
| -
|
27 |
| - rectangles = [ |
28 |
| - [1,1,2,3], |
29 |
| - [1,3,2,4], |
30 |
| - [3,1,4,2], |
31 |
| - [3,2,4,4] |
32 |
| - ] |
33 |
| -
|
34 |
| - Return false. Because there is a gap between the two rectangular regions. |
35 |
| -
|
36 |
| - Example 3: |
37 |
| -
|
38 |
| - rectangles = [ |
39 |
| - [1,1,3,3], |
40 |
| - [3,1,4,2], |
41 |
| - [1,3,2,4], |
42 |
| - [3,2,4,4] |
43 |
| - ] |
44 |
| -
|
45 |
| - Return false. Because there is a gap in the top center. |
46 |
| -
|
47 |
| - Example 4: |
48 |
| -
|
49 |
| - rectangles = [ |
50 |
| - [1,1,3,3], |
51 |
| - [3,1,4,2], |
52 |
| - [1,3,2,4], |
53 |
| - [2,2,4,4] |
54 |
| - ] |
55 |
| -
|
56 |
| - Return false. Because two of the rectangles overlap with each other. |
57 |
| - */ |
58 | 6 | publicclass_391 {
|
59 | 7 | publicstaticclassSolution1 {
|
60 |
| -/** credit: https://discuss.leetcode.com/topic/56052/really-easy-understanding-solution-o-n-java */ |
| 8 | +/** |
| 9 | + * credit: https://discuss.leetcode.com/topic/56052/really-easy-understanding-solution-o-n-java |
| 10 | + */ |
61 | 11 | publicbooleanisRectangleCover(int[][]rectangles) {
|
62 | 12 | if (rectangles.length ==0 ||rectangles[0].length ==0) {
|
63 | 13 | returnfalse;
|
@@ -99,7 +49,7 @@ public boolean isRectangleCover(int[][] rectangles) {
|
99 | 49 | }
|
100 | 50 |
|
101 | 51 | if (!set.contains(x1 +" " +y1) || !set.contains(x1 +" " +y2) || !set.contains(
|
102 |
| -x2 +" " +y1) || !set.contains(x2 +" " +y2) ||set.size() !=4) { |
| 52 | +x2 +" " +y1) || !set.contains(x2 +" " +y2) ||set.size() !=4) { |
103 | 53 | returnfalse;
|
104 | 54 | }
|
105 | 55 |
|
|