Leetcode 223: Rectangle Area

Find the total area covered by two rectilinear rectangles in a 2D plane.

Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.

Rectangle Area

Assume that the total area is never beyond the maximum possible value of int.

 

 1 public class Solution {
 2     public int ComputeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
 3         int area1 = (C - A) * (D - B), area2 = (G - E) * (H - F);
 4         
 5         if (C <= E || D <= F || A >= G || B >= H) 
 6         {
 7             return area1 + area2;
 8         }
 9         else
10         {
11             return area1 + area2 - (Math.Min(C, G) -  Math.Max(A, E)) * (Math.Min(D, H) -  Math.Max(B, F));    
12         }
13     }
14 }

 

 

posted @ 2017-12-03 07:18  逸朵  阅读(108)  评论(0编辑  收藏  举报