223. Rectangle Area

来源:互联网 发布:qq定位软件 编辑:程序博客网 时间:2024/06/18 05:58

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.

想找出重合部分 ,再将总的部分减去场合的面积。

public class Solution {

    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
            int l=0;
            int d=0;
            l=A<=E?helper(A,C,E,G):helper(E,G,A,C);
            d=B<=F?helper(B,D,F,H):helper(F,H,B,D);
            return total(A,B,C,D)+total(E,F,G,H)-l*d;
    }
    public int helper(int a,int b,int c,int d){
    return b<c?0:(b<d?b:d)-c;
    }
    public int total(int a,int b,int c,int d){
      return (c-a)*(d-b);
    }
}
0 0
原创粉丝点击