Skip to content

Commit c167cc2

Browse files
author
liwentian
committed
fd
1 parent 1857e2b commit c167cc2

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@
271271
|384|[Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/ShuffleAnArray.java)|75|
272272
|387|[First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/FirstUniqueCharacterInAString.java)|100|
273273
|388|[Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/LongestAbsoluteFilePath.java)|65|其实不难,乍看没思路,多做几遍|
274-
|391|[Perfect Rectangle](https://leetcode.com/problems/perfect-rectangle/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/PerfectRectangle.java)||
274+
|391|[Perfect Rectangle](https://leetcode.com/problems/perfect-rectangle/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/PerfectRectangle.java)|70|这题很trick,多做两遍|
275275
|393|[UTF-8 Validation](https://leetcode.com/problems/utf-8-validation/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/UTFValidation.java)|60|错了很多次,多做几遍|
276276
|394|[Decode String](https://leetcode.com/problems/decode-string/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/DecodeString.java)|75|这题容易错,多做四遍|
277277
|395|[Longest Substring with At Least K Repeating Characters](https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/LongestSubstringWithAtLeastKRepeatingCharacters.java)|65|
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.leetcode.google;
2+
3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
6+
/**
7+
* Created by liwentian on 2017/8/31.
8+
*/
9+
10+
public class PerfectRectangle {
11+
12+
public boolean isRectangleCover(int[][] rectangles) {
13+
14+
int x1 = Integer.MAX_VALUE, y1 = Integer.MAX_VALUE;
15+
int x2 = 0, y2 = 0;
16+
17+
HashSet<String> set = new HashSet<>();
18+
19+
int area = 0;
20+
21+
for (int[] rect : rectangles) {
22+
int left = rect[0];
23+
int bottom = rect[1];
24+
int right = rect[2];
25+
int top = rect[3];
26+
27+
area += (right - left) * (top - bottom);
28+
29+
x1 = Math.min(left, x1);
30+
y1 = Math.min(bottom, y1);
31+
x2 = Math.max(right, x2);
32+
y2 = Math.max(top, y2);
33+
34+
String s1 = left + "." + bottom;
35+
String s2 = left + "." + top;
36+
String s3 = right + "." + bottom;
37+
String s4 = right + "." + top;
38+
39+
if (!set.add(s1)) {
40+
set.remove(s1);
41+
}
42+
43+
if (!set.add(s2)) {
44+
set.remove(s2);
45+
}
46+
47+
if (!set.add(s3)) {
48+
set.remove(s3);
49+
}
50+
51+
if (!set.add(s4)) {
52+
set.remove(s4);
53+
}
54+
}
55+
56+
String s1 = x1 + "." + y1;
57+
String s2 = x1 + "." + y2;
58+
String s3 = x2 + "." + y1;
59+
String s4 = x2 + "." + y2;
60+
61+
if (!set.contains(s1) || !set.contains(s2) || !set.contains(s3) || !set.contains(s4) || set.size() != 4) {
62+
return false;
63+
}
64+
65+
return area == (x2 - x1) * (y2 - y1);
66+
}
67+
}

0 commit comments

Comments
 (0)