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

Commit 2eb248a

Browse files
committed
#836 矩形重叠
1 parent b0fd3cc commit 2eb248a

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package math;
2+
3+
/**
4+
* @author liangqian0723@gmail.com
5+
* @since 2020/3/18 3:16 PM
6+
*/
7+
public class LC_836_RectangleOverlap {
8+
/**
9+
* 思路:rec1左下角的坐标都比rec2右上角大,rec1右上角的坐标都比rec2左下角大,
10+
*/
11+
public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
12+
return rec1[2] > rec2[0] && // left
13+
rec1[3] > rec2[1] && // bottom
14+
rec1[0] < rec2[2] && // right
15+
rec1[1] < rec2[3]; // top
16+
}
17+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package math;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.core.Is.is;
6+
import static org.junit.Assert.assertThat;
7+
8+
/**
9+
* @author liangqian0723@gmail.com
10+
* @since 2020/3/18 3:15 PM
11+
*/
12+
public class LC_836_RectangleOverlapTest {
13+
@Test
14+
public void test_is_over_lap() {
15+
LC_836_RectangleOverlap rl = new LC_836_RectangleOverlap();
16+
assertThat(rl.isRectangleOverlap(new int[]{0, 0, 2, 2}, new int[]{1, 1, 3, 3}), is(true));
17+
}
18+
19+
@Test
20+
public void test_is_over_lap_case2() {
21+
LC_836_RectangleOverlap rl = new LC_836_RectangleOverlap();
22+
assertThat(rl.isRectangleOverlap(new int[]{0, 0, 1, 1}, new int[]{1, 0, 2, 1}), is(false));
23+
}
24+
25+
@Test
26+
public void test_is_over_lap_case3() {
27+
LC_836_RectangleOverlap rl = new LC_836_RectangleOverlap();
28+
assertThat(rl.isRectangleOverlap(new int[]{5, 15, 8, 18}, new int[]{0, 3, 7, 9}), is(false));
29+
}
30+
}

0 commit comments

Comments
 (0)