Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f7d398a

Browse files
refactor 391
1 parent 913eb76 commit f7d398a

File tree

1 file changed

+44
-41
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+44
-41
lines changed

src/main/java/com/fishercoder/solutions/_391.java

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -56,51 +56,54 @@
5656
Return false. Because two of the rectangles overlap with each other.
5757
*/
5858
public class _391 {
59-
/**reference: https://discuss.leetcode.com/topic/56052/really-easy-understanding-solution-o-n-java*/
60-
public boolean isRectangleCover(int[][] rectangles) {
61-
if (rectangles.length == 0 || rectangles[0].length == 0) {
62-
return false;
63-
}
64-
65-
int x1 = Integer.MAX_VALUE;
66-
int x2 = Integer.MIN_VALUE;
67-
int y1 = Integer.MAX_VALUE;
68-
int y2 = Integer.MIN_VALUE;
69-
70-
Set<String> set = new HashSet<>();
71-
int area = 0;
72-
73-
for (int[] rect : rectangles) {
74-
x1 = Math.min(rect[0], x1);
75-
y1 = Math.min(rect[1], y1);
76-
x2 = Math.max(rect[2], x2);
77-
y2 = Math.max(rect[3], y2);
78-
79-
area += (rect[2] - rect[0]) * (rect[3] - rect[1]);
80-
81-
String s1 = rect[0] + " " + rect[1];
82-
String s2 = rect[0] + " " + rect[3];
83-
String s3 = rect[2] + " " + rect[3];
84-
String s4 = rect[2] + " " + rect[1];
85-
86-
if (!set.add(s1)) {
87-
set.remove(s1);
88-
}
89-
if (!set.add(s2)) {
90-
set.remove(s2);
59+
public static class Solution1 {
60+
/** credit: https://discuss.leetcode.com/topic/56052/really-easy-understanding-solution-o-n-java */
61+
public boolean isRectangleCover(int[][] rectangles) {
62+
if (rectangles.length == 0 || rectangles[0].length == 0) {
63+
return false;
9164
}
92-
if (!set.add(s3)) {
93-
set.remove(s3);
65+
66+
int x1 = Integer.MAX_VALUE;
67+
int x2 = Integer.MIN_VALUE;
68+
int y1 = Integer.MAX_VALUE;
69+
int y2 = Integer.MIN_VALUE;
70+
71+
Set<String> set = new HashSet<>();
72+
int area = 0;
73+
74+
for (int[] rect : rectangles) {
75+
x1 = Math.min(rect[0], x1);
76+
y1 = Math.min(rect[1], y1);
77+
x2 = Math.max(rect[2], x2);
78+
y2 = Math.max(rect[3], y2);
79+
80+
area += (rect[2] - rect[0]) * (rect[3] - rect[1]);
81+
82+
String s1 = rect[0] + " " + rect[1];
83+
String s2 = rect[0] + " " + rect[3];
84+
String s3 = rect[2] + " " + rect[3];
85+
String s4 = rect[2] + " " + rect[1];
86+
87+
if (!set.add(s1)) {
88+
set.remove(s1);
89+
}
90+
if (!set.add(s2)) {
91+
set.remove(s2);
92+
}
93+
if (!set.add(s3)) {
94+
set.remove(s3);
95+
}
96+
if (!set.add(s4)) {
97+
set.remove(s4);
98+
}
9499
}
95-
if (!set.add(s4)) {
96-
set.remove(s4);
100+
101+
if (!set.contains(x1 + " " + y1) || !set.contains(x1 + " " + y2) || !set.contains(
102+
x2 + " " + y1) || !set.contains(x2 + " " + y2) || set.size() != 4) {
103+
return false;
97104
}
98-
}
99105

100-
if (!set.contains(x1 + " " + y1) || !set.contains(x1 + " " + y2) || !set.contains(x2 + " " + y1) || !set.contains(x2 + " " + y2) || set.size() != 4) {
101-
return false;
106+
return area == (x2 - x1) * (y2 - y1);
102107
}
103-
104-
return area == (x2 - x1) * (y2 - y1);
105108
}
106109
}

0 commit comments

Comments
 (0)