Skip to content

Commit f36eaa8

Browse files
committed
add rect area
1 parent eca06c9 commit f36eaa8

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

rectangle-area/README.md

Whitespace-only changes.

rectangle-area/Solution.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
public class Solution {
2+
3+
int area(int x1, int y1, int x2, int y2){
4+
return (y2 - y1) * (x2 - x1);
5+
}
6+
7+
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
8+
9+
// make sure left is left
10+
if(A > E){
11+
return computeArea(E, F, G, H, A, B, C, D);
12+
}
13+
14+
int a = area(A, B, C, D) + area(E, F, G, H);
15+
16+
// no share case1
17+
// [ ]C
18+
// E[ ]
19+
if(C < E){
20+
return a;
21+
}
22+
23+
// no share case2
24+
// [ ]B
25+
//
26+
// [ ]H
27+
28+
if(B > H || F > D){
29+
return a;
30+
}
31+
32+
// remove share
33+
return a - area(E, Math.max(B, F), Math.min(C, G), Math.min(D, H));
34+
}
35+
}

rectangle-area/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
layout: solution
3+
title: Rectangle Area
4+
date: 2015-06-08 23:32:33+08:00
5+
leetcode_id: 223
6+
---
7+
{% include_relative README.md %}

0 commit comments

Comments
 (0)